MongoDB查多个条件的$pull / $unset的查询
为此,请使用$pull和update。让我们创建一个包含文档的集合-
> db.demo198.insertOne({"List":{"Values":[10,20,30,30,70,80,90]}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c224503d395bdc21346df") } > db.demo198.insertOne({"List":{"Values":[56,978,56,34,23,34]}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c225403d395bdc21346e0") } > db.demo198.insertOne({"List":{"Values":[21,12,14,15,34,56]}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c226603d395bdc21346e1") }
在find()方法的帮助下显示集合中的所有文档-
> db.demo198.find();
这将产生以下输出-
{ "_id" : ObjectId("5e3c224503d395bdc21346df"), "List" : { "Values" : [ 10, 20, 30, 30, 70, 80, 90 ] } } { "_id" : ObjectId("5e3c225403d395bdc21346e0"), "List" : { "Values" : [ 56, 978, 56, 34, 23, 34 ] } } { "_id" : ObjectId("5e3c226603d395bdc21346e1"), "List" : { "Values" : [ 21, 12, 14, 15, 34, 56 ] } }
以下是对具有多个条件的$pull/$unset的查询-
> db.demo198.update({},{ "$pull": { "List.Values": { "$lt": 40 } } },{ "multi": true }); WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
在find()方法的帮助下显示集合中的所有文档-
> db.demo198.find();
这将产生以下输出-
{ "_id" : ObjectId("5e3c224503d395bdc21346df"), "List" : { "Values" : [ 70, 80, 90 ] } } { "_id" : ObjectId("5e3c225403d395bdc21346e0"), "List" : { "Values" : [ 56, 978, 56 ] } } { "_id" : ObjectId("5e3c226603d395bdc21346e1"), "List" : { "Values" : [ 56 ] } }