快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

如何进行MongoDB排除查询$ne缺陷

本篇文章给大家分享的是有关如何进行MongoDB排除查询$ne缺陷,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联建站长期为成百上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为缙云企业提供专业的成都做网站、成都网站建设,缙云网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。

    最近学习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缺陷,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


文章名称:如何进行MongoDB排除查询$ne缺陷
文章分享:http://6mz.cn/article/ihgcsj.html

其他资讯