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