mysql - python: query mongodb database to find a specific value under a unknown field -
i using python generate mongodb database collection , need find specific values database, document like:
{ "_id":objectid(215454541245), "category":food "venues":{"thai restaurant":251, "kfc":124, "chinese restaurant":21,.....} }
my question that, want query database , find venues have value smaller 200, in example, "kfc" , "chinese restaurant" returned query.
anyone knows how that?
if can change schema easier issue queries against collection. is, having dynamic values keys considered bad design pattern mongodb extremely difficult query.
a recommended approach follow embedded model this:
{ "_id": objectid("553799187174b8c402151d06"), "category": "food", "venues": [ { "name": "thai restaurant", "value": 251 }, { "name": "kfc", "value": 124 }, { "name": "chinese restaurant", "value": 21 } ] }
thus structure issue query find venues have value smaller 200:
db.collection.findone({"venues.value": { "$lt": 200 } }, { "venues": { "$elemmatch": { "value": { "$lt": 200 } } }, "_id": 0 });
this return result:
/* 0 */ { "venues" : [ { "name" : "kfc", "value" : 124 } ] }
Comments
Post a Comment