MongoDB查询返回指定字段

一、初始化数据

{ item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }, instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ]

二、只返回指定字段和_id字段

db.collection.find( { status: "A" }, { item: 1, status: 1 } )

三、查询结果去掉_id字段

db.collection.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )

四、查询结果返回不包含指定字段

db.collection.find( { status: "A" }, { status: 0, instock: 0 } )

五、查询内嵌文档(数组对象)返回指定字段

db.collection.find({ status: "A" },{ "size.uom": 0 ,instock.qty:1})

六、查询返回数组中指定数组对象

db.collection.find( { status: "A" }, { name: 1, status: 1, instock: { $slice: -1 } } )//instock数组的最后一个值

 

posted @ 2019-04-09 15:26  yudis  阅读(28595)  评论(0编辑  收藏  举报