查询返回的字段显示($,$slice,$elemMatch)

1.查看容器中的所有文档
db.inventory.find()
{ "_id" : ObjectId("5446ebddd251b112db5bf7bc"), 
  "item" : "ABC1", 
  "details" : {"model" : "14Q3","manufacturer" : "XYZ Company"}, 
  "stock" : [ { "size" : "S", "qty" : 25 },{ "size" : "M", "qty" : 50 } ], 
  "category" : "clothing" }
{ "_id" : ObjectId("5446ebfbd251b112db5bf7bd"), 
  "item" : "ABC2", 
  "details" : { "model" : "14Q2", "manufacturer" : "XYZ Company" }, 
  "stock" : [ { "size" : "S", "qty" : 30 }, { "size" : "M", "qty" : 60 } ], 
  "category" : "clothing" }
{ "_id" : ObjectId("5446ec08d251b112db5bf7be"), 
  "item" : "ABC3", 
  "details" : { "model" : "14Q4", "manufacturer" : "XYZ Company" }, 
  "stock" : [ { "size" : "S", "qty" : 10 }, { "size" : "M", "qty" : 20 } ], 
  "category" : "clothing" }

2.查询返回指定的字段
> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 ,"stock": 1})
{ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 }, { "size" : "M", "qty" : 50 } ] }

3.数组字段查询返回的显示($slice)
> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: 1}}) //返回顺数第一个元素
{ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 } ] }

> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: 2}})
{ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 }, { "size" : "M", "qty" : 50 } ] }

> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: 3}})
{ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 }, { "size" : "M", "qty" : 50 } ] }

> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: 0}})
{ "item" : "ABC1", "stock" : [ ] }

> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: -1}}) //返回倒数第一个元素
{ "item" : "ABC1", "stock" : [ { "size" : "M", "qty" : 50 } ] }

> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: -2}})
{ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 }, { "size" : "M", "qty" : 50 } ] }

> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: [-1,1]}}) //$silce:[skip,limit]
{ "item" : "ABC1", "stock" : [ { "size" : "M", "qty" : 50 } ] }
//返回一项从倒数第一个开始返回

> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: [1,1]}}) //$silce:[skip,limit]
{ "item" : "ABC1", "stock" : [ { "size" : "M", "qty" : 50 } ] }
//return 1 items, after skipping the first 1 items of that array.

> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: [-2,1]}}) //$silce:[skip,limit]
{ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 } ] }
//返回一项从倒数第二个开始返回

4.$elemMatch在数组查询中的使用
The $elemMatch operator limits the contents of an <array> field from the query results to 
contain only the first element matching the $elemMatch condition.//返回第一个满足条件的元素(如果有多个元素满足条件)

> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$elemMatch: {"size": "S" } }})
{ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 } ] }

> db.inventory.find({},{"item":1,"_id": 0 , "stock": {$elemMatch: {"size": "S" } }})
{ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 } ] }
{ "item" : "ABC2", "stock" : [ { "size" : "S", "qty" : 30 } ] }
{ "item" : "ABC3", "stock" : [ { "size" : "S", "qty" : 10 } ] }

> db.inventory.find({},{"item":1,"_id": 0 , "stock": {$elemMatch: {"qty": {$gt: 20} } }})
{ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 } ] }
{ "item" : "ABC2", "stock" : [ { "size" : "S", "qty" : 30 } ] }
{ "item" : "ABC3" }

> db.inventory.find({},{"item":1,"_id": 0 , "stock": {$elemMatch: {"size": "S" } }}).sort({"stock.qty": -1})
{ "item" : "ABC2", "stock" : [ { "size" : "S", "qty" : 30 } ] }
{ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 } ] }
{ "item" : "ABC3", "stock" : [ { "size" : "S", "qty" : 10 } ] }

5.$在数组查询中的使用
db.collection.find( { <array>: <value> ... },
                    { "<array>.$": 1 } )
db.collection.find( { <array.field>: <value> ...},
                    { "<array>.$": 1 } )
The <array> field being limited must appear in the query document, 
and the <value> can be documents that contain query operator expressions	
				
> db.inventory.find({"stock.qty": {$gt: 10}},{"stock.$": 1}) //stock必须作为查询条件的字段 并且返回的只是数组中第一个满足条件的元素
{ "_id" : ObjectId("5446ebddd251b112db5bf7bc"), "stock" : [ { "size" : "S", "qty" : 25 } ] }
{ "_id" : ObjectId("5446ebfbd251b112db5bf7bd"), "stock" : [ { "size" : "S", "qty" : 30 } ] }
{ "_id" : ObjectId("5446ec08d251b112db5bf7be"), "stock" : [ { "size" : "M", "qty" : 20 } ] }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值