从mongodb集合中提取所有_id的最佳方法是什么?我正在使用pymongo与mongodb合作.以下代码:
for item in db.some_collection.find({}, {'_id': 1}):
# do something
需要一些时间来迭代所有集合.我只需要_id值,它们都应该适合记忆.为什么这段代码不能立即完成?
解决方法:
使用distinct
:
some_collection.distinct('_id')
In [5]: c = pymongo.connection.Connection('127.0.0.1')
In [6]: c['test']['test'].insert({'a': 2})
Out[6]: ObjectId('5159c8e9d286da0efccb7b70')
In [7]: c['test']['test'].insert({'a': 3})
Out[7]: ObjectId('5159c8ecd286da0efccb7b71')
In [8]: c['test']['test'].insert({'a': 5})
Out[8]: ObjectId('5159c8edd286da0efccb7b72')
In [9]: c['test']['test'].distinct('_id')
Out[9]:
[ObjectId('5159c8e9d286da0efccb7b70'),
ObjectId('5159c8ecd286da0efccb7b71'),
ObjectId('5159c8edd286da0efccb7b72')]