How to make a query in this nested document structure (MongoDB)?

ぃ、小莉子 提交于 2019-12-05 11:59:32
RameshVel

I have answered this couple of times about picking up the sub-documents alone from mongo collection here, and here

Simply there is no way to do this currently. This is the behavior of filtering multi level embedded document, normally the matching filter would return the whole document, not the subsets.

There are two outstanding issues already in mongo related to this positional ($) operator in fields to return specifier and Ability to make use of a subdocument's data whose contents were used to satisfy a query using the $ operator. (Please login to vote if you really needed the feature)

And your alternate schema also not useful here.

so you have to store the each feature in separate document like this to make it work the way you wanted

feature 1

{
'_id': SomeObjectId,
'name' :'some name',
'value': 'feature 1',
'some_field' : 'zzz'
}

feature 2

{
'_id': SomeObjectId,
'name' :'some name',
'value': 'feature 2',
'some_field' : 'zzz'
}

and querying

db.features.find({'_id':someobjectid})

will only return the specific feature

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!