MongoDB的排除查詢$ne缺陷

清風艾艾發表於2017-07-04

    最近學習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
作業系統是:Red Hat Enterprise Linux Server release 6.4 (Santiago)




來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29357786/viewspace-2141567/,如需轉載,請註明出處,否則將追究法律責任。

相關文章