一、初始化数据

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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-31
  • 2022-01-30
  • 2022-12-23
  • 2021-12-11
  • 2022-12-23
  • 2021-10-26
猜你喜欢
  • 2021-12-15
  • 2022-12-23
  • 2021-06-02
  • 2021-07-18
  • 2022-02-22
  • 2022-12-23
相关资源
相似解决方案