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陣列的最後一個值
[admin via ] MongoDB查詢返回指定欄位已經有825次圍觀