dws*_*ein 2 python mongodb pymongo
我有以下代码:
client = MongoClient()
data_base = client.hkpr_restore
agents_collection = data_base.agents
agent_ids = agents_collection.find({},{"_id":1})
Run Code Online (Sandbox Code Playgroud)
这给了我一个结果:
{u'_id': ObjectId('553020a8bf2e4e7a438b46d9')}
{u'_id': ObjectId('553020a8bf2e4e7a438b46da')}
{u'_id': ObjectId('553020a8bf2e4e7a438b46db')}
Run Code Online (Sandbox Code Playgroud)
我如何获取 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']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9067 次 |
| 最近记录: |