python - Pymongo:无法从 mongodb 中找到记录
问题描述
我有一个包含国家/地区记录的集合,我需要找到带有 uid 的特定国家,它是 countryId
以下是样本采集数据:
{
"uid": 15024,
"countries": [{
"countryId": 123,
"popullation": 45000000
},
{
"countryId": 456,
"poppulation": 9000000000
}
]
},
{
"uid": 15025,
"countries": [{
"countryId": 987,
"popullation": 560000000
},
{
"countryId": 456,
"poppulation": 8900000000
}
]
}
我在 python 中尝试了以下查询,但找不到任何结果:
foundRecord = collection.find_one({"uid" : 15024, "countries.countryId": 456})
但它返回无。
请帮助和建议。
解决方案
您确定您使用的是相同的数据库/集合源吗?
似乎您正在将结果保存在另一个集合中。
我试图重现您的问题,它适用于我的 mongodb(请注意,我使用的是 v4)
编辑:很高兴有你定义“集合”的代码