MongoDB的排除查询$ne缺陷

  1. 云栖社区>
  2. 博客列表>
  3. 正文

MongoDB的排除查询$ne缺陷

kissweety 2017-07-04 10:26:16 浏览168 评论0

摘要:     最近学习MongoDB数据库基本开发知识,发现MongoDB的排除查$ne类似于Oracle的not in 或者not exists, 但是$ne只能对字符串键值对起作用而对数字类型不行,举例如下: 示例数据: > d...

    最近学习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)




用云栖社区APP,舒服~

【云栖快讯】云栖社区技术交流群汇总,阿里巴巴技术专家及云栖社区专家等你加入互动,老铁,了解一下?  详情请点击

网友评论