如何查询pymongo db的object_ids列表?

问题描述 投票:0回答:1

我想查询一个MongoDB,但查询的是一系列的 _ids.

例如,我试图查询一个MongoDB,但要查询一个_ids的列表。

db.test.find(ObjectId(['4ecc05e55dd98a436ddcc47c',4ecc05e55dd98a436adfc47c', ....] ))
python database pymongo
1个回答
0
投票

您需要使用 $in 操作员,例如

db.test.find({'_id': {'$in': [ObjectId("4ecc05e55dd98a436ddcc47c"), ObjectId("4ecc05e55dd98a436adfc47c")]}})

这在Mongo shell和pymongo中都可以使用。


0
投票

声明你的列表。

list_of_ids = [ObjectId("4ecc05e55dd98a436ddcc47c"),ObjectId("4ecc05e55dd98a436adfc47c")]

并运行 db.test.find({'_id': {'$in': list(list_of_ids)}}.

© www.soinside.com 2019 - 2024. All rights reserved.