MongoDB

Get Started
Official Manual

借用官网的例子数据:db.inventory

{ item: "journal", qty: 25, status: "A", size: { h: 14, w: 21, uom: "cm" }, tags: ["blank", "red"], dim_cm: [ 14, 21 ] }
{ item: "notebook", qty: 50, status: "A", size: { h: 8.5, w: 11, uom: "in" }, tags: ["red", "blank"], dim_cm: [ 14, 21 ] }
{ item: "paper", qty: 100, status: "D", size: { h: 8.5, w: 11, uom: "in" }, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] }
{ item: "planner", qty: 75, status: "D", size: { h: 22.85, w: 30, uom: "cm" }, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] }
{ item: "postcard", qty: 45,  status: "A", size: { h: 10, w: 15.25, uom: "cm" }, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
查找所有:
db.inventory.find( {} )
简单匹配普通属性:
db.inventory.find( { status: "D" } )

使用操作符匹配普通属性:

db.inventory.find( { status: { $in: [ "A", "D" ] } } )

多个属性匹配,之间是且的关系:

db.inventory.find( { status: "A", qty: { $lt: 30 } } )

或的关系需要另加操作符:

db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )

且 和 或 共同使用的时:

db.inventory.find( {  
    status: "A",  
    $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]  } )
查找,数组/对象属性中有满足匹配的子项
db.inventory.find( { tags: "red" } )
db.inventory.find( { "size.uom": "in" } )

数组/对象完全匹配查找(子项顺序必须完全相同,不然匹配不到)

db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
db.inventory.find( { tags: ["red", "blank"] } )
匹配数组时注意:默认多元素子项可分摊匹配
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )

The following example queries for documents where the dim_cm array contains elements that in some combination satisfy the query conditions; e.g., one element can satisfy the greater than 15 condition and another element can satisfy the less than 20 condition, or a single element can satisfy both.

匹配数组:期望元素子项内匹配
db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )

The following example queries for documents where the dim_cm array contains at least one element that is both greater than ($gt) 22 and less than ($lt) 30.

匹配数组可以用索引(索引从0开始)
db.inventory.find( { "dim_cm.1": { $gt: 25 } } )
匹配数组,可以多元素捆绑匹配
db.inventory.find( { tags: { $all: ["red", "blank"] } } )

同(既包含'red', 也包含'blank'的):

db.inventory.find({ $and: [ { tags: "red" }, { tags: "blank" } ] })

匹配对象数组

转载于:https://my.oschina.net/elleneye/blog/841027

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值