在MongoDB中删除子集合中的对象?
要删除子集合中的对象,请在MongoDB中使用$pull。让我们创建一个包含文档的集合-
> db.demo715.insertOne({
... _id:1,
... details :
... [
... { 'id' : '101',
... 'Information' : [
... {
... 'studentid' : '102',
... "Name":"Bob"
... },
... {
... 'studentid' : '103',
... "Name":"Chris"
... }
... ]
... }
... ]
... });
{ "acknowledged" : true, "insertedId" : 1 }在find()方法的帮助下显示集合中的所有文档-
> db.demo715.find();
这将产生以下输出-
{ "_id" : 1, "details" : [ { "id" : "101", "Information" : [ { "studentid" : "102", "Name" : "Bob" }, { "studentid" : "103", "Name" : "Chris" } ] } ] }以下是在MongoDB的子集合中删除对象的查询-
> db.demo715.update(
... {"details.id":'101'},
... { $pull: { 'details.$.Information':{studentid:'102',"Name":"Bob"} } }
... );
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })在find()方法的帮助下显示集合中的所有文档-
> db.demo715.find();
这将产生以下输出-
{ "_id" : 1, "details" : [ { "id" : "101", "Information" : [ { "studentid" : "103", "Name" : "Chris" } ] } ] }热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便