重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

如何进行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://cqcxhl.com/article/ihgcsj.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP