[blockquote]

输入db,显示你正在操作的数据库;切换数据库,输入use dbName,如果数据库不存在的话会自动帮我们创建一个;使用show dbs可以显示所有可用的数据库。

[/blockquote]

测试数据在文末

插入文档

插入操作的行为表现

_id字段:在MongoDB中,存储于集合中的文档需要一个唯一的_id字段作为primary key。如果文档中没有指定_id字段,MongoDB将使用ObjectId作为_id字段的默认值;也就是说,如果文档在插入时没有包含顶层的_id字段,MongoDB驱动将添加持有ObjectId的_id字段。

插入

db.users.insertOne():向集合插入单个文档 3.2新功能

db.users.insertOne({name:"webb",age:23})

插入成功后会返回对应的ObjectId:

{"acknowledged" : true,"insertedId" :ObjectId("590b237b0207f4bae6db100a")}

db.users.insertMany():向集合插入多个文档 3.2新功能

db.users.insertMany([{name:"lebo",age:22},{name:"jack",age:24}])

插入成功后会返回对应的ObjectId:

{"acknowledged" : true,"insertedIds" : [ObjectId("590b24f00207f4bae6db100b"),ObjectId("590b24f00207f4bae6db100c")]}

db.collection.insert():向集合插入一个或多个文档,想要插入一个文档,传递一个文档给该方法;想要插入多个文档,传递文档数组给该方法。

db.users.insert({name:"webb",age:24})

该操作返回了含有操作状态的 WriteResult 对象.插入文档成功返回如下 WriteResult 对象:

WriteResult({ "nInserted" : 1 })

nInserted 字段指明了插入文档的总数.如果该操作遇到了错误, WriteResult 对象将包含该错误信息。

向users集合插入多个文档:

db.users.insert([{name:"lebo",age:25},{name:"jack",age:26}])

该方法返回了包含操作状态的 BulkWriteResult 对象.成功插入文档返回如下 BulkWriteResult 对象:

BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 2,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})

查询文档

条件查询

查找当前集合所有数据:

db.users.find({})
db.users.find()

指定查询条件:

db.users.find({age:22})

使用$in后面的查询条件(也可以使用or替代):

db.users.find({age:{$in:[22,23]}})

复合查询:

db.users.find({name:"ahn", age:22})

使用or:

db.users.find({$or:[{status:"A"},{age:{$lt:30}}]})

返回查询的映射字段

映射文档:映射文档限制了返回所有匹配文档的字段。映射文档可以指明包括哪些字段或者排除哪些字段:

{ field1: <value>, field2: <value> ... }

value:

1或true在返回的文档中包含字段

0或false排除该字段

只返回指定字段及_id字段:

db.users.find({status:"A"}, {name:1, status:1})

只返回指定字段:

db.users.find({status:"A"}, {name:1, status:1,_id:0})

返回除了排除字段外的字段:

db.users.find({status:"A"}, {name:0, status:0})

返回嵌入文档中的指定字段:

db.users.find({status:"A"}, {name:1,status:1,"favorites.food":1})

查询值为null或者不存在的字段

插入测试数据:

db.users.insert([{ "_id" : 900, "name" : null },{ "_id" : 901 }])

相等过滤器:将返回两个文档。如果该查询使用了sparse索引,不管怎样,那么该查询将仅仅匹配 null 值,而不匹配不存在的字段。

db.users.find({name:null})

类型筛查:查询仅仅匹配那些包含值是null的name字段的文档。

db.users.find({name:{$type:10}})

存在行筛查:只返回没有name字段的文档。

db.users.find({name:{$exists:false}})

更新文档

行为表现

_id字段:一旦设定,你不能更新_id字段的值,你也不能用有不同_id字段值的替换文档替换已经存在的文档。

文档大小:当执行更新操作增加了文档大小,超过了为该文档分配的空间时,更新操作会在磁盘上重新定位该文档。

更新文档中指定字段

db.collection.updateOne():更新匹配过滤条件第一个匹配的文档 3.2新功能

使用$set操作符更新指定的字段

使用$currentDate操作符更新lastModified字段的值到当前时间

db.users.updateOne({"name":"sue"},{$set:{type:3},$currentDate:{lastModified:true}})

db.collection.updateMany():更新所有匹配过滤条件的文档 3.2新功能

db.users.updateMany({"name":"lebo"},{$set:{age:22},$currentDate:{lastModified:true}})

db.collection.update():更新匹配过滤条件第一个匹配的文档

db.users.update({name:"webb"},{$set:{age:21},$currentDate:{lastModified:true}})

文档替换

db.collection.replaceOne():将匹配过滤条件的第一个文档替换为新文档。 3.2新功能

db.users.replaceOne({name:"webb"},{name:"webb",age:21})

db.collection.update():将匹配过滤条件的第一个文档替换为新文档。

db.users.update({name:"xyz"},{name:"mee", age:25, type:1, status:"A"} )

删除文档

删除所有文档

db.collection.deleteMany() 3.2新功能

db.users.deleteMany({})

db.collection.remove()

db.users.remove({})

删除满足条件的所有文档

db.collection.deleteMany() 3.2新功能

db.users.deleteMany({name:"lebo"})

db.collection.remove():删除所有type为1的文档。

db.users.remove({type:1})

仅删除一个满足条件的文档

db.collection.deleteOne() 3.2新功能

db.users.deleteOne({name:"webb"})

db.collection.remove():将

db.users.remove({name:"webb"},1)

测试数据:

db.users.insertMany(
[
 {
   _id: 1,
   name: "sue",
   age: 19,
   type: 1,
   status: "P",
   favorites: { artist: "Picasso", food: "pizza" },
   finished: [ 17, 3 ],
   badges: [ "blue", "black" ],
   points: [
      { points: 85, bonus: 20 },
      { points: 85, bonus: 10 }
   ]
 },
 {
   _id: 2,
   name: "bob",
   age: 42,
   type: 1,
   status: "A",
   favorites: { artist: "Miro", food: "meringue" },
   finished: [ 11, 25 ],
   badges: [ "green" ],
   points: [
      { points: 85, bonus: 20 },
      { points: 64, bonus: 12 }
   ]
 },
 {
   _id: 3,
   name: "ahn",
   age: 22,
   type: 2,
   status: "A",
   favorites: { artist: "Cassatt", food: "cake" },
   finished: [ 6 ],
   badges: [ "blue", "red" ],
   points: [
      { points: 81, bonus: 8 },
      { points: 55, bonus: 20 }
   ]
 },
 {
   _id: 4,
   name: "xi",
   age: 34,
   type: 2,
   status: "D",
   favorites: { artist: "Chagall", food: "chocolate" },
   finished: [ 5, 11 ],
   badges: [ "red", "black" ],
   points: [
      { points: 53, bonus: 15 },
      { points: 51, bonus: 15 }
   ]
 },
 {
   _id: 5,
   name: "xyz",
   age: 23,
   type: 2,
   status: "D",
   favorites: { artist: "Noguchi", food: "nougat" },
   finished: [ 14, 6 ],
   badges: [ "orange" ],
   points: [
      { points: 71, bonus: 20 }
   ]
 },
 {
   _id: 6,
   name: "abc",
   age: 43,
   type: 1,
   status: "A",
   favorites: { food: "pizza", artist: "Picasso" },
   finished: [ 18, 12 ],
   badges: [ "black", "blue" ],
   points: [
      { points: 78, bonus: 8 },
      { points: 57, bonus: 7 }
   ]
 }
]
)