我有一个集合,我只想根据MongoDB环境中存在的另一个密钥检索特定值。
这是我正在做的事情:
db.results.find({'someKeyThatShouldExist':{$exists:true}}, {"parentKey.childKey.theKeyWoseValueIwant":1}
这为我生成以下格式的数据:
{ "_id" : ObjectId("532a2c2b6803fa486b8b456a"), "parentKey" : { "childKey" : { "theKeyWhoseValueIWant" : 102982577 }}}.....
现在,我真正想要的只是价值102982577,而不是其他所有东西。 我怎么能这样做?
答案 0 :(得分:1)
您可以通过将_id:0
添加到投影条件来取消_id。
db.results.find(
{"someKeyThatShouldExist":{$exists:true}},
{_id:0, "parentKey.childKey.theKeyWoseValueIwant":1}
)
要获得价值,您可以执行以下操作:
db.results.find(
{"someKeyThatShouldExist":{$exists:true}},
{_id:0, "parentKey.childKey.theKeyWoseValueIwant":1}
)[0].parentKey.childKey.theKeyWoseValueIwant