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