gpt4 book ai didi

python - 如何使用 pymongo 获取仅包含 ObjectId 的列表?

转载 作者:行者123 更新时间:2023-12-05 08:57:44 24 4
gpt4 key购买 nike

我有以下代码:

client = MongoClient()
data_base = client.hkpr_restore
agents_collection = data_base.agents
agent_ids = agents_collection.find({},{"_id":1})

这给了我一个结果:

{u'_id': ObjectId('553020a8bf2e4e7a438b46d9')}
{u'_id': ObjectId('553020a8bf2e4e7a438b46da')}
{u'_id': ObjectId('553020a8bf2e4e7a438b46db')}

我如何才能获取 ObjectId,然后才能使用每个 ID 搜索另一个集合?

最佳答案

使用distinct

In [27]: agent_ids = agents_collection.distinct('_id')

In [28]: agent_ids
Out[28]:
[ObjectId('553662940acf450bef638e6d'),
ObjectId('553662940acf450bef638e6e'),
ObjectId('553662940acf450bef638e6f')]

In [29]: agent_id2 = [str(id) for id in agents_collection.distinct('_id')]

In [30]: agent_id2
Out[30]:
['553662940acf450bef638e6d',
'553662940acf450bef638e6e',
'553662940acf450bef638e6f']

关于python - 如何使用 pymongo 获取仅包含 ObjectId 的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29771192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com