MongoDB-查询嵌入文档,但投影显示键/值的路径?
所以基本上我想要MongoDB-查询嵌入文档,但投影显示键/值的路径?,mongodb,mongodb-query,Mongodb,Mongodb Query,所以基本上我想要 db.scoreFacts.find( {"instrumentRanges.flute.minPitch": {$gte: 0, $lte:56}}, {"instrumentRanges.flute.minPitch": 1}) 归来 { "_id" : "Bach_Brandenburg5_Mov1.xml", "minPitch" : 50 } 但我得到的却是: { "_id" : "Bach_Brandenburg5_Mov1.xml", "instr
db.scoreFacts.find(
{"instrumentRanges.flute.minPitch": {$gte: 0, $lte:56}},
{"instrumentRanges.flute.minPitch": 1})
归来
{ "_id" : "Bach_Brandenburg5_Mov1.xml", "minPitch" : 50 }
但我得到的却是:
{ "_id" : "Bach_Brandenburg5_Mov1.xml", "instrumentRanges" : { "flute" : { "minPitch" : 50 } } }
本质上,返回“minPitch”的路径,这不是我需要的。仅使用.find()(无映射等)如何实现所需的输出?谢谢。使用标准的
.find()
查询无法完成此操作。如果希望更改文档结构,请研究使用aggregate()
调用。然后可以使用投影定义所需的结果字段
例如:
db.scoreFacts.aggregate([
{ $match: {"instrumentRanges.flute.minPitch": {$gte: 0, $lte:56}} },
{ $project: {"minPitch": "$instrumentRanges.flute.minPitch"} }
]);
有关更多信息,请参阅。此外,请查看先决条件
注意:我自己还没有测试过上述查询,因此您可能需要对其进行一些修改,以获得所需的行为