您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何進行MongoDB排除查詢$ne缺陷,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
最近學習MongoDB數據庫基本開發知識,發現MongoDB的排除查$ne類似于Oracle的not in 或者not exists,
但是$ne只能對字符串鍵值對起作用而對數字類型不行,舉例如下:
示例數據:
> dvd=({"Type":"DVD","Title":"Matrix,The","Released":1999,"Cast":["Keanu Reeeves","Carrie-Anne Moss","Laurence Fishburne","Hugo Weaving","Gloria Foster","Joe Pantoliano"]})
{
"Type" : "DVD",
"Title" : "Matrix,The",
"Released" : 1999,
"Cast" : [
"Keanu Reeeves",
"Carrie-Anne Moss",
"Laurence Fishburne",
"Hugo Weaving",
"Gloria Foster",
"Joe Pantoliano"
]
}
> db.media.insert(dvd)
WriteResult({ "nInserted" : 1 })
> dvd=({"Type":"DVD","Title":"Blade Runner","Released":1982})
{ "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 }
> db.media.insert(dvd)
WriteResult({ "nInserted" : 1 })
> dvd=({"Type":"DVD","Title":"Toy Story 3","Released":2010})
{ "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 }
> db.media.insert(dvd)
WriteResult({ "nInserted" : 1 })
>
正常的字符串排除查詢,以示例數據中Title鍵進行測試:
> db.media.find({"Type":"DVD","Title":{$ne:"Blade Runner"}})
{ "_id" : ObjectId("595af5ce39297105e4322fbd"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1999, "Cast" : [ "Keanu Reeeves", "Carrie-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster", "Joe Pantoliano" ] }
{ "_id" : ObjectId("595af63239297105e4322fbf"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 }
>
對數字排除無效,以示例數據中Released鍵進行測試:
> db.media.find({"Type":"DVD","Release":{$ne:"1999"}})
{ "_id" : ObjectId("595af5ce39297105e4322fbd"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1999, "Cast" : [ "Keanu Reeeves", "Carrie-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster", "Joe Pantoliano" ] }
{ "_id" : ObjectId("595af61939297105e4322fbe"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 }
{ "_id" : ObjectId("595af63239297105e4322fbf"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 }
>
> db.media.find({"Type":"DVD","Release":{$ne:1999}})
{ "_id" : ObjectId("595af5ce39297105e4322fbd"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1999, "Cast" : [ "Keanu Reeeves", "Carrie-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster", "Joe Pantoliano" ] }
{ "_id" : ObjectId("595af61939297105e4322fbe"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 }
{ "_id" : ObjectId("595af63239297105e4322fbf"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 }
>
實驗結果:mongodb的$ne操作只能對字符串鍵值對發揮作用。
本次實驗的MongoDB數據庫版本是:3.2.7
以上就是如何進行MongoDB排除查詢$ne缺陷,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。