nested queries in pymongo using collection.find()

我与影子孤独终老i 提交于 2019-12-07 16:13:57

问题


I would like to create a data base using mongodb and I am wondering how to query nested entities. For instance, let's assume we create a db as follows:

from pymongo import MongoClient
db = client['test_database']
collection = db['test_collection']
dat=[  
 { "id":110, "data":{"Country":"ES","Count":64}},
 { "id":112, "data":{"Country":"ES","Count":5}},
 { "id":114, "data":{"Country":"UK","Count":3}}
]
collection.insert(dat)

how can we query the records with "Country" values "ES"? Or alternatively how can we query the records with "Count" less than 6?


回答1:


You can use the dot notation supported by mongo.

db.test_collection.find({"data.Country": "ES"})
db.test_collection.find({"data.Count": {"$lt": 6}})

Check this stackoverflow question for the non-Python version.



来源:https://stackoverflow.com/questions/18515195/nested-queries-in-pymongo-using-collection-find

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