我有一个包含国家记录的集合,我需要找到具有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})
但它返回无。
请帮助和建议。
我认为以下会更好:
foundRecord = collection.find_one({"uid" : 15024,
"countries" : {"$elemMatch" : { "countryId" : 456 }})
您确定要使用相同的数据库/收集源吗?
似乎您将结果保存在另一个集合中。
我试图重现你的问题,它适用于我的mongodb(注意我正在使用v4)
编辑:很高兴有你要定义“集合”的代码片段