MongoDB 查询基础

澳门新葡亰平台游戏网站 1

MongoDB Compass 是 MongoDB
的可视化学工业具,用它开拓表中的数目后,如何过滤展现呢?

数量策动

{ "goods_id" : 1, "goods_name" : "KD876", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : "4", "goods_name" : "诺基亚N85原装充电器", "createTime" : ISODate("2016-09-11T00:00:00Z") }{ "goods_id" : 3, "goods_name" : "诺基亚原装5800耳机", "createTime" : ISODate("2016-10-09T00:00:00Z") }{ "goods_id" : 5, "goods_name" : "索爱原装M2卡读卡器", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 6, "goods_name" : "胜创KINGMAX内存卡", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 7, "goods_name" : "诺基亚N85", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 8, "goods_name" : "飞利浦9@9v", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 9, "goods_name" : "诺基亚E66", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : "10", "goods_name" : "索爱C702c", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 11, "goods_name" : "索爱C702c", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 12, "goods_name" : "摩托罗拉A810", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 13, "goods_name" : "诺基亚5320", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 14, "goods_name" : "诺基亚5800XM", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : "15", "goods_name" : "摩托罗拉A810", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 16, "goods_name" : "恒基伟业G101", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 17, "goods_name" : "夏新N7", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 18, "goods_name" : "夏新T5", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 19, "goods_name" : "三星SGH-F258", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 20, "goods_name" : "三星BC01", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 2, "createTime" : ISODate("2015-10-01T00:00:00Z") }

上边有三个 FILTEENVISION,当中的根底语法是 JSON,举例:{ size: 3 }。

find

find的首先个参数决定了要赶回哪些文书档案,用于钦命询问条件。要不钦点询问文书档案,暗中同意正是{},内定两个键/值对,约等于sql的and。第一个参数来钦赐想要的键(暗中同意情形下,”_id”总是显得)。

澳门新葡亰平台游戏网站 1

询问条件

上面表示只呈现 _id 为指定 ObjectId 的记录。

And查询

接受AND型查询时,应尽可能用起码的标准化来节制结果的限制。

> db.product.find({"goods_id":"4","goods_name":"诺基亚N85原装充电器"},{"_id":0}){ "goods_id" : "4", "goods_name" : "诺基亚N85原装充电器", "createTime" : ISODate("2016-09-11T00:00:00Z") }

理所当然也足以如此(纯属闲得蛋疼):

> db.product.find({"$and":[{"goods_id":"4","goods_name":"诺基亚N85原装充电器"}]},{"_id":0}){ "goods_id" : "4", "goods_name" : "诺基亚N85原装充电器", "createTime" : ISODate("2016-09-11T00:00:00Z") }

若是要运用 <、>、=、<=、>=、in
那么些咋办吧?分别用:$lt、$gt、$eq、$lte、$gte、$in。

Or查询

方式一:$in、$nin

澳门新葡亰平台游戏网站,$in能够用来询问叁个键的四个值,可以钦定区别等级次序的规格和值。

$nin将回来与数组中具备准则都不相配的文档。

$in是对单个键做O宝马X5查询。

> db.product.find({"goods_id":{"$in":["4",5,6]}},{"_id":0}){ "goods_id" : "4", "goods_name" : "诺基亚N85原装充电器", "createTime" : ISODate("2016-09-11T00:00:00Z") }{ "goods_id" : 5, "goods_name" : "索爱原装M2卡读卡器", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 6, "goods_name" : "胜创KINGMAX内存卡", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }> 

方式二:$or

$or更通用一些,能够在多少个键中查询大肆的给定值

动用$or时,第多少个规格规范化应尽或然相配越来越多的文档,那样才是最最迅猛的。

> db.product.find({"$or":[{"goods_id":16},{"goods_name":"夏新T5"},{"createTime":{"$lt":new Date("2016-01-01")}}]},{"_id":0}){ "goods_id" : 16, "goods_name" : "恒基伟业G101", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 18, "goods_name" : "夏新T5", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 2, "createTime" : ISODate("2015-10-01T00:00:00Z") }

{ sequence : { $lt : 30 } }

$lt、 $lte、 $gt、 $gte、$eq(貌似没什么用)、$ne

$lt、 $lte、 $gt、 $gte、$eq、$ne分别对应sql中的<、<=
、>、>=、=、!=。组合查找多少个节制的值。

> db.product.find({"goods_id":{"$gte":3,"$lt":5}},{"_id":0} ){ "goods_id" : 3, "goods_name" : "诺基亚原装5800耳机", "createTime" : ISODate("2016-10-09T00:00:00Z") }



> start=new Date("01/01/2016") ISODate("2015-12-31T16:00:00Z")> db.product.find({"createTime":{"$lt":start}})//查询指定日期之前的数据{ "_id" : ObjectId("585a65d9a847c6d3a3ee1da5"), "goods_id" : 2, "createTime" : ISODate("2015-10-01T00:00:00Z") }

{ organizationCode : { $in : [“C11”, “C12”] } }

限制

询问利用上有个别节制。传递给数据库的查询文书档案的值必需是常量。也便是说无法援引文书档案中其他键的值。

 db.product.find({"goods_id":this.goods_name}).pretty()

MongoDB 不像关系型数据库那么珍惜,所以那边 in 能够用字符串。

$mod

取模运算符,会将查询的值除以第叁个给定值,若余数等于第贰个给定的值则相称成功。

> db.product.find({"goods_id":{"$mod":[5,1]}},{"_id":0}){ "goods_id" : 1, "goods_name" : "KD876", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 6, "goods_name" : "胜创KINGMAX内存卡", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 11, "goods_name" : "索爱C702c", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 16, "goods_name" : "恒基伟业G101", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }> 

有关大小写

$not

$not是元典型句,即能够用在别的任何其余标准之上。查询和合营的件相反的多寡。

$not与正则表明式联合利用时极为有用,用来搜寻这一个与特定形式不协作的文书档案。

> db.product.find({"goods_id":{"$not":{"$mod":[5,1]}}},{"_id":0}){ "goods_id" : "4", "goods_name" : "诺基亚N85原装充电器", "createTime" : ISODate("2016-09-11T00:00:00Z") }{ "goods_id" : 3, "goods_name" : "诺基亚原装5800耳机", "createTime" : ISODate("2016-10-09T00:00:00Z") }{ "goods_id" : 5, "goods_name" : "索爱原装M2卡读卡器", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 7, "goods_name" : "诺基亚N85", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 8, "goods_name" : "飞利浦9@9v", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 9, "goods_name" : "诺基亚E66", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : "10", "goods_name" : "索爱C702c", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 12, "goods_name" : "摩托罗拉A810", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 13, "goods_name" : "诺基亚5320", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 14, "goods_name" : "诺基亚5800XM", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : "15", "goods_name" : "摩托罗拉A810", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 17, "goods_name" : "夏新N7", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 18, "goods_name" : "夏新T5", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 19, "goods_name" : "三星SGH-F258", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 20, "goods_name" : "三星BC01", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 2, "createTime" : ISODate("2015-10-01T00:00:00Z") }

{ myName : “a” }、{ myName: { $eq : “a” }} 、{ myName: { $in : [ “a” ]
} }

标准语义

在询问中,$lt在内层文书档案,而在改正中$inc则是外围文书档案的键。

基本得以不容争辩:条件语句是内层文档的键,而改过器则是外围文书档案的键。

叁个键方可有私下多个原则,不过贰个键不能够对应多少个更新校勘器。

有部分”元操作符”也放在外层文书档案中,比如$and、$or、$nor

询问优化器不会$and进行优化,那与别的操作符不等同。

高低写敏感,上边只可以查出 myName 为小写 a 的记录。

特定类型的查询

还有 and、or 等

null

null不止会相配有些键的值为null的文书档案,并且还有也许会合作不含那一个键的文书档案。

借使仅想相配键值为null的文书档案,既要检查该键的值是不是为null,还要经过$exists条件判定键值已存在。

> db.product.update({"goods_id":2},{"$set":{"goods_name":null}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.product.find({"goods_name":null},{"_id":0}){ "goods_id" : 2, "createTime" : ISODate("2015-10-01T00:00:00Z"), "goods_name" : null }



> db.product.find({"goods_no":{"$in":[null],"$exists":true}},{"_id":0})

{$or: [{ organizationCode : “a” }, { sequence: 10 }] }

正则表达式

正则表达式能够灵活有效地相称字符串,系统能够吸收接纳正则表达式标识i
(忽视大小写卡塔尔,但不自然要有。

MongoDB可以为前缀型正则表明式(比方:/^joey/卡塔尔查询创立索引,所以那种类型的查询会特别便捷。

> db.product.find({"goods_name":/^三星/},{"_id":0}){ "goods_id" : 19, "goods_name" : "三星SGH-F258", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }{ "goods_id" : 20, "goods_name" : "三星BC01", "createTime" : ISODate("2016-12-21T11:19:39.010Z") }

正则表明式也足以相配本人。

> db.product.find({"goods_name":/baz/},{"_id":0}){ "goods_id" : 3, "goods_name" : /baz/, "createTime" : ISODate("2016-10-09T00:00:00Z") }

{$and: [{ organizationCode : “a” }, { sequence: 10 }] }

询问数组

查询数组元素与查询标量值是如出风华正茂辙的。

> db.product.update({"goods_id":"10"},{"$push":{"goods_type":{"$each":["华为", "乐视", "小米"]}}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.product.find({"goods_type":"小米"},{"_id":0}){ "goods_id" : "10", "goods_name" : "索爱C702c", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "华为", "乐视", "小米" ] }

and 能够简写,不应用数组,如下:

$all

由此四个要平素相称数组,那样就能合作豆蔻梢头组成分。

> db.product.update({"goods_id":20},{"$push":{"goods_type":{"$each":["三星", "苹果", "努比亚"]}}})> db.product.update({"goods_id":18},{"$push":{"goods_type":{"$each":["华为", "苹果", "魅族"]}}})> db.product.find({"goods_type":{"$all":["苹果","华为"]}},{"_id":0}){ "goods_id" : 18, "goods_name" : "夏新T5", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "华为", "苹果", "魅族" ] }

当然也足以使用一切数组进行正确相称。不过,正确相称必得大同小异的才查的出来,比方顺序,个数都得千篇生龙活虎律。

> db.product.find({"goods_type":["华为","苹果","魅族"]},{"_id":0}){ "goods_id" : 18, "goods_name" : "夏新T5", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "华为", "苹果", "魅族" ] }

{ organizationCode : “a” , sequence: 10 }

检索特定岗位的元素

要想查询数组特定岗位的成分,需利用key.index语法内定下标,数组下标皆以从0先河的。

> db.product.find({"goods_type.1":"苹果"},{"_id":0}){ "goods_id" : 18, "goods_name" : "夏新T5", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "华为", "苹果", "魅族" ] }{ "goods_id" : 20, "goods_name" : "三星BC01", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "三星", "苹果", "努比亚" ] }> 

也正是说,钦赐 $and、$or 是跟叁个数组,数组中的每一类是字段名与值,而简写
and 间接用三个 JSON 对象,此中包罗种种字段名与值。

$size

$size查询特定长度的数组

> db.product.find({"goods_type":{"$size":3}},{"_id":0}){ "goods_id" : "10", "goods_name" : "索爱C702c", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "华为", "乐视", "小米" ] }{ "goods_id" : 18, "goods_name" : "夏新T5", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "华为", "苹果", "魅族" ] }{ "goods_id" : 20, "goods_name" : "三星BC01", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "三星", "苹果", "努比亚" ] }> 

$slice

$slice操作符能够回到有些键相配的数组成分的两个子集。那么些操作符是用来拿到数组的前多少个只怕后多少个因素,对全数数据开展过滤未有效应(比如: db.product.find({},{“goods_type”:{“$slice”:2}}卡塔尔(قطر‎ 会返回文书档案的具备数据)

> db.product.find({"goods_type":{"$size":3}},{"goods_type":{"$slice":-2},"_id":0}){ "goods_id" : "10", "goods_name" : "索爱C702c", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "乐视", "小米" ] }{ "goods_id" : 18, "goods_name" : "夏新T5", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "苹果", "魅族" ] }{ "goods_id" : 20, "goods_name" : "三星BC01", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "苹果", "努比亚" ] }> db.product.find({"goods_type":{"$size":3}},{"goods_type":{"$slice":2},"_id":0}){ "goods_id" : "10", "goods_name" : "索爱C702c", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "华为", "乐视" ] }{ "goods_id" : 18, "goods_name" : "夏新T5", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "华为", "苹果" ] }{ "goods_id" : 20, "goods_name" : "三星BC01", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "三星", "苹果" ] }> 

$slice也可以钦定偏移值以致希望重返的因素数量,来回到成分集合中间地方的少数结果。

> db.product.find({"goods_type":{"$size":3}},{"goods_type":{"$slice":[1,1]},"_id":0})//跳过1个,取1个{ "goods_id" : "10", "goods_name" : "索爱C702c", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "乐视" ] }{ "goods_id" : 18, "goods_name" : "夏新T5", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "苹果" ] }{ "goods_id" : 20, "goods_name" : "三星BC01", "createTime" : ISODate("2016-12-21T11:19:39.010Z"), "goods_type" : [ "苹果" ] }

回来三个极度的数组成分

$ 再次回到与查询条件相相配的任性一个数组成分。只会回去第二个十分的文书档案。

> db.blog.find({"comments.votes":3}).pretty(){    "_id" : ObjectId("585694e4c5b0525a48a441b5"),    "content" : "...",    "comments" : [        {            "comment" : "good post",            "author" : "jim",            "votes" : 1        },        {            "comment" : "i thought it was too short",            "author" : "claire",            "votes" : 3        },        {            "comment" : "free watches",            "author" : "alice",            "votes" : 3        }    ]}> > db.blog.find({"comments.votes":3},{"comments.$":1}).pretty(){    "_id" : ObjectId("585694e4c5b0525a48a441b5"),    "comments" : [        {            "comment" : "i thought it was too short",            "author" : "claire",            "votes" : 3        }    ]}

数组和限量查询的相互影响

文书档案中的标量(非数组成分)必需与查询条件中的每一条语句相相称。比方,假诺选取 {“$gt”:10,”$lt”:20} 举行查询,只会同盟x键的值超过10同期小于20的文书档案。

不过,假诺有个别文书档案的x字段是多少个数组,借使x键的某叁个因素与查询条件拿到大肆一条语句相匹配,那么那些文书档案也会被重返。

{ "_id" : ObjectId("585d404b53fe663a4c5c202e"), "x" : 5 }{ "_id" : ObjectId("585d404b53fe663a4c5c202f"), "x" : 15 }{ "_id" : ObjectId("585d404b53fe663a4c5c2030"), "x" : 25 }{ "_id" : ObjectId("585d404b53fe663a4c5c2031"), "x" : [ 5, 25 ] }> db.foo.find({"x":{"$gt":5,"$lt":20}}){ "_id" : ObjectId("585d404b53fe663a4c5c202f"), "x" : 15 }{ "_id" : ObjectId("585d404b53fe663a4c5c2031"), "x" : [ 5, 25 ] }

上述方法对数组使用节制查询未有用:范围会同盟自便四个要素数组。有两种办法可以获得预期的结果。

方式1:$elemMatch

$elemMatch要求MongoDB同期接纳查询条件中的多个语句与一个数组元素举行相比。可是,它不会同盟非数组成分。

> db.foo.find({"x":{"$elemMatch":{"$gt":10,"$lt":20}}})> 

方式2:使用min()和max()

一经当前询问的字段上开创过索引,能够动用min(State of Qatar和max(卡塔尔国将查询条件遍历的目录范围节制为”$gt”和”$lt”的值。何况,必得为这么些目录的富有字段钦命min(卡塔尔(قطر‎和max().

> db.foo.find({"x":{"$gt":10,"$lt":20}}).min({"x":10}).max({"x":20})
You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图