python - MongoDB query doesn't behave as expected with nested elements -
i'm trying return results match value , match on child properties. want return results match month , match range of values. find simple search, doesn't seem return results expect.
using pymongo query is:
month = 2 results = db.master.find({"months": str(month)}) this should surely return matching documents correct month. no records returns month=2
my data stored in mdb as:
{ "_id": { "$oid": "568d0bebc1bed847da7a2e6f" }, "months": { "2": { "std_rank": 0.11338862902393358, "rank_gain": 0.6183933826187626, "gain": 0.9618213660245183, "std": 0.021891473641317716 }, "months": { "3": { "std_rank": 0.11338862902393358, "rank_gain": 0.6183933826187626, "gain": 0.9618213660245183, "std": 0.021891473641317716 }, }, "code": "vu" } one suggested answer works filtering correct month, question how apply filter child elements. example:
results = db.master.find({}, {"_id": 0, "months." + str(month): 1, "months.std_rank": {"$lte": max_std_rank, "$gte": min_std_rank} } ) i following error:
pymongo.errors.operationfailure: database error: can't canonicalize query: badvalue >1 field in obj: { $gte: 0.0, $lte: 1.0 }
try following query
results = db.master.find({ "months." + str(month) + ".std_rank": {"$lte": max_std_rank, "$gte": min_std_rank } }, { "_id": 0, "months." + str(month): 1 } ) which uses projection return document matching given month key.
Thank you so much for this post
ReplyDeletePython Online Training