首页 > 解决方案 > 如何在嵌入文档中找到某个元素

问题描述

有没有什么办法只返回B级商店的字段?

我试过了:db.restaurant.find( {"Result.Grade" : "B"} ) 但它会返回所有 B 级文档内容。

谢谢!!

标签: mongodb

解决方案


查询中的第一个参数find用于过滤器。使用第二个参数并传递您要检索的字段。

db.restaurant.find(
  { "Result.Grade" : "B" },   //filter
  { "Name": 1, "Number": 1 }      //projection
)

使用 mongodb 节点驱动使用.project()游标方法

db.restaurant.find(
  { "Result.Grade" : "B" },
).project({ "Name": 1, "Number": 1 })

推荐阅读