Mongodb 查询反转,为返回除了特定文档之外的所有项?
要获取除了某些特定文档之外的文档,请将 $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 }
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP