要获取除某些特定文档以外的文档,请使用$nor和$and。首先让我们创建一个包含文档的集合-
> db.demo1.insertOne({"StudentName":"Chris","StudentMarks":38}); { "acknowledged" : true, "insertedId" : ObjectId("5e08a4f025ddae1f53b62216") } > db.demo1.insertOne({"StudentName":"David","StudentMarks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5e08a4f725ddae1f53b62217") } > db.demo1.insertOne({"StudentName":"Mike","StudentMarks":96}); { "acknowledged" : true, "insertedId" : ObjectId("5e08a4fd25ddae1f53b62218") }
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.demo1.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5e08a4f025ddae1f53b62216"), "StudentName" : "Chris", "StudentMarks" : 38 } { "_id" : ObjectId("5e08a4f725ddae1f53b62217"), "StudentName" : "David", "StudentMarks" : 78 } { "_id" : ObjectId("5e08a4fd25ddae1f53b62218"), "StudentName" : "Mike", "StudentMarks" : 96 }
这是获取查询逆的查询-
> db.demo1.find({$nor:[{$and:[{'StudentName':'David'},{'StudentMarks':78}]}]});
这将产生以下输出。结果显示带有78以外的分数的学生记录-
{ "_id" : ObjectId("5e08a4f025ddae1f53b62216"), "StudentName" : "Chris", "StudentMarks" : 38 } { "_id" : ObjectId("5e08a4fd25ddae1f53b62218"), "StudentName" : "Mike", "StudentMarks" : 96 }