Arrays 在mongodb中匹配数组中的字段时,如何忽略其他字段?

Arrays 在mongodb中匹配数组中的字段时,如何忽略其他字段?,arrays,mongodb,mongoose,mongodb-query,Arrays,Mongodb,Mongoose,Mongodb Query,收藏是 { _id: 1, results: [ { product: "abc", score: 10 }, { product: "xyz", score: 5 } ] } { _id: 2, results: [ { product: "abc", score: 8 }, { product: "xyz", score: 7 } ] } { _id: 3, results: [ { product: "abc", score: 7 }, { product: "xyz", score: 8

收藏是

{ _id: 1, results: [ { product: "abc", score: 10 }, { product: "xyz", score: 5 } ] }
{ _id: 2, results: [ { product: "abc", score: 8 }, { product: "xyz", score: 7 } ] }
{ _id: 3, results: [ { product: "abc", score: 7 }, { product: "xyz", score: 8 } ] }
疑问是

db.survey.find(
   { results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } }
)
它回来了

{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 }, { "product" : "xyz", "score" : 8 } ] }


但我还想在返回json时忽略{“product”:“abc”,“score”:7}。我也尝试了其他匹配,但它仍然会与其他记录一起返回。如何处理?感谢您的帮助。

您需要使用位置运算符
$
投影匹配结果,否则它将始终返回与查询匹配的完整文档。(这就是为什么你会得到完整的结果)

db.survey.find(
{结果:{$elemMatch:{产品:“xyz”,分数:{$gte:8}},
{“结果$”:1}
)

这是否回答了您的问题?