我想知道如何在 PyMongo API 中按顺序执行复杂的查询。我有一个数据库,我想检索数据。数据库特征为{id, username, ...}
,id包含{uid, timestamp}
。我想执行检查特定 uid
的查询。我尝试过类似的方法:
db = client.test_database
db = client['trendSetters']
collection = db.test_collection
collection = db["cms_users_features"]
result = collection.find_one()
for col in collection.find({"_id": {"uid": 702993}}):
print col
编辑
for col in collection.find({"_id": {"uid": 702993, "timestamp":1393554289813 }}):
print col
如果我添加时间戳
,我就可以执行查询。在不添加时间戳
的情况下如何做到这一点?
最佳答案
您可以通过在父字段名称和嵌套字段名称之间添加点来访问嵌套字段:
parent_field.child_field
这是解决方案:
for col in collection.find({"_id.uid": 702993}):
print col
关于python - PyMongo - 按一个嵌套字段匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23363808/