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数组的最后一个值

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM