Section 2.1: 创建

db.people.insert({name: 'Tom', age: 28});

Or

db.people.save({name: 'Tom', age: 28});

与保存不同的是,如果传入的文档包含 _id 字段,那么如果已经存在带有该 _id 的文档,则该文档将被更新,而不是作为新文档添加。

MongoDB 3.2.x 中新增了两种将文档插入集合的方法:

使用 insertOne 只插入一条记录:

db.people.insertOne({name: 'Tom', age: 28});

使用 insertMany 插入多条记录:

db.people.insertMany([{name: 'Tom', age: 28},{name: 'John', age: 25}, {name: 'Kathy', age: 23}])

请注意,自 3.0 版起,insert 在所有官方语言驱动程序中都被强调为已废弃。其全部区别在于,shell 方法在实现该方法方面实际上落后于其他驱动程序。同样的情况也适用于所有其他 CRUD 方法

Section 2.2: 更新

更新整个对象:

db.people.update({name: 'Tom'}, {age: 29, name: 'Tom'})
// New in MongoDB 3.2
db.people.updateOne({name: 'Tom'},{age: 29, name: 'Tom'}) //Will replace only first matching
document.
db.people.updateMany({name: 'Tom'},{age: 29, name: 'Tom'}) //Will replace all matching documents.

或者只更新文档的一个字段。在本例中就是年龄:

db.people.update({name: 'Tom'}, {$set: {age: 29}})

通过添加第三个参数,您还可以同时更新多个文档。此查询将更新名称等于汤姆的所有文档:

db.people.update({name: 'Tom'}, {$set: {age: 29}}, {multi: true})
// New in MongoDB 3.2
db.people.updateOne({name: 'Tom'},{$set:{age: 30}) //Will update only first matching document.
db.people.updateMany({name: 'Tom'},{$set:{age: 30}}) //Will update all matching documents.

如果有新字段需要更新,该字段将被添加到文档中。

db.people.updateMany({name: 'Tom'},{$set:{age: 30, salary:50000}})  // Document will have `salary` field as well.

如果需要更换文件、

db.collection.replaceOne({name:'Tom'}, {name:'Lakmal',age:25,address:'Sri Lanka'})

可以使用。

注意:用于标识对象的字段将保存在更新后的文档中。更新部分未定义的字段将从文档中删除。

Section 2.3: 删除

删除所有与查询参数匹配的文件:

// New in MongoDB 3.2
db.people.deleteMany({name: 'Tom'})
// All versions
db.people.remove({name: 'Tom'})

或只有一个

// New in MongoDB 3.2
db.people.deleteOne({name: 'Tom'})
// All versions
db.people.remove({name: 'Tom'}, true)

MongoDB 的 remove() 方法。如果执行该命令时不带任何参数或空参数,它将删除集合中的所有文档。

db.people.remove();

或

db.people.remove({});

Section 2.4: 读取

查询人物集合中所有名称字段值为 “Tom ”的文档:

db.people.find({name: 'Tom'})

或者只查询第一个:

db.people.findOne({name: 'Tom'})

您还可以通过传递字段选择参数来指定要返回的字段。下文将排除 _id 字段,只包含 age 字段:

db.people.find({name: 'Tom'}, {_id: 0, age: 1})

注意:默认情况下,即使你不要求返回 _id 字段,也会返回。如果不想返回 _id,可以按照前面的示例,通过指定 _id: 0(或 _id:false)。如果要查找包含国家、城市等信息的子记录,如地址对象。

db.people.find({'address.country': 'US'})

如果需要,也可以指定字段

db.people.find({'address.country': 'US'}, {'name': true, 'address.city': true}) 
// Remember that the result has a `.pretty()` method that pretty-prints resulting JSON:
db.people.find().pretty()

Section 2.5: 更新内嵌文件

对于以下模式

{name: 'Tom', age: 28, marks: [50, 60, 70]}

将汤姆的分数更新为 55,其中分数为 50(使用位置运算符 $):

db.people.update({name: "Tom", marks: 50}, {"$set": {"marks.$": 55}})

对于以下模式

{name: 'Tom', age: 28, marks: [{subject: "English", marks: 90},{subject: "Maths", marks: 100},
{subject: "Computes", marks: 20}]}

将汤姆的英语分数更新为 85 分 :

db.people.update({name: "Tom", "marks.subject": "English"},{"$set":{"marks.$.marks": 85}})

解释上述示例:

使用 {name: “Tom”, “marks.subject”: “English"},您将获得对象在标记数组中的位置,其中主题为英语。在“marks.$.marks”中,$ 用于更新标记数组中该位置的值。

更新数组中的值

位置 $ 操作符可识别数组中要更新的元素,而无需明确指定元素在数组中的位置。 考虑一个包含以下文档的学生集合:

{ "_id" : 1, "grades" : [ 80, 85, 90 ] }
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }

要将第一个文档中成绩数组中的 80 更新为 82,如果不知道元素在数组中的位置,可以使用位置 $ 操作符:

db.students.update(
{ _id: 1, grades: 80 },
{ $set: { "grades.$" : 82 } }
)

Section 2.6: 更多更新操作符

在更新文档时,除了 $set 操作符外,您还可以使用其他操作符。操作符 $push 可以将一个值推入一个数组,在本例中,我们将在昵称数组中添加一个新的昵称。

db.people.update({name: 'Tom'}, {$push: {nicknames: 'Tommy'}})
// This adds the string 'Tommy' into the nicknames array in Tom's document.

$pull “操作符与”$push "操作符相反,您可以从数组中提取特定项。

db.people.update({name: 'Tom'}, {$pull: {nicknames: 'Tommy'}})
// This removes the string 'Tommy' from the nicknames array in Tom's document.

$pop 操作符允许你从数组中移除第一个或最后一个值。假设 'Tom' 的文档中有一个名为 “兄弟姐妹 ”的属性,其值为['Marie', 'Bob', 'Kevin', 'Alex']。

db.people.update({name: 'Tom'}, {$pop: {siblings: -1}})
// This will remove the first value from the siblings array, which is 'Marie' in this case.
db.people.update({name: 'Tom'}, {$pop: {siblings: 1}})
// This will remove the last value from the siblings array, which is 'Alex' in this case.

Section 2.7: 更新多个文档时的 “multi ”参数

要更新集合中的多个文档,请将 multi 选项设为 true。

db.collection.update(
query,
update,
{
upsert: boolean,
multi: boolean,
writeConcern: document
}
)

multi 是可选项。如果设置为 true,则更新多个符合查询条件的文档。如果设置为 false,则更新一个文档。默认值为 false。

db.mycol.find() { "_id" : ObjectId(598354878df45ec5), "title":"MongoDB Overview"} { "_id" :
ObjectId(59835487adf45ec6), "title":"NoSQL Overview"} { "_id" : ObjectId(59835487adf45ec7),
"title":"Tutorials Point Overview"}
db.mycol.update({'title':'MongoDB Overview'}, {$set:{'title':'New MongoDB Tutorial'}},{multi:true})