简体   繁体   English

Pymongo:无法从mongodb中找到记录

[英]Pymongo: Unable to find record from mongodb

I have a collection containing country records, I need to find particular country with uid and it's countryId 我有一个包含国家/地区记录的集合,我需要使用uid查找特定的国家/地区,即countryId

Below is the sample collection data: 以下是样本收集数据:

{
"uid": 15024,

"countries": [{
        "countryId": 123,
        "popullation": 45000000

    },
    {
        "countryId": 456,
        "poppulation": 9000000000
    }
]

},
{
"uid": 15025,

"countries": [{
        "countryId": 987,
        "popullation": 560000000

    },
    {
        "countryId": 456,
        "poppulation": 8900000000
    }
]

}

I have tried with below query in in python but unable to find any result: 我已经尝试在python中使用以下查询,但无法找到任何结果:

foundRecord = collection.find_one({"uid" : 15024, "countries.countryId": 456})

but it return None. 但它返回None。

Please help and suggest. 请帮助和建议。

I think following will work better : 我认为以下方法会更好:

foundRecord = collection.find_one({"uid" : 15024,
                                   "countries" : {"$elemMatch" : { "countryId" : 456 }})

Are you sure you're using the same Database / Collection source? 您确定使用相同的数据库/集合源吗?

Seems that you're saving results on another collection. 似乎您要将结果保存在另一个集合中。

I've tried to reproduce your problem and it works on my mongodb ( note that I'm using v4) 我试图重现您的问题,它可以在我的mongodb上正常工作(请注意,我正在使用v4)

EDIT: Would be nice to have the piece of code where you're defining "collection" 编辑:最好在您定义“集合”的地方有一段代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM