Home > other >  Python - the best way to extract all the _id from mongo collection
Python - the best way to extract all the _id from mongo collection

Time:02-05

Extract all _id from mongo collection is the best way? I'm using pymongo cooperate with mongo. The following code:

For the item in the db. Some_collection. Find ({}, {" _id ": 1}) :
# do something

Need some time to iterate over all set. All I need is _id value, they should be suitable for memory. Why is this code is not completed immediately?

CodePudding user response:

Using distinct:

Some_collection. Distinct (" _id ")

In [5] : c=pymongo. Connection. The connection (' 127.0.0.1)

In [6], [c] 'test' [' test '] insert ({' a ': 2})
The Out [6] : ObjectId (' 5159 c8e9d286da0efccb7b70)

In [7], [c] 'test' [' test '] insert ({' a ': 3})
The Out [7] : ObjectId (' 5159 c8ecd286da0efccb7b71)

In [8] : [c] 'test' [' test '] insert ({' a ': 5})
The Out [8] : ObjectId (' 5159 c8edd286da0efccb7b72)

In [9] : [c] 'test' [' test ']. Distinct (" _id ")
The Out [9] :
[ObjectId (' 5159 c8e9d286da0efccb7b70),
The ObjectId (' 5159 c8ecd286da0efccb7b71),
The ObjectId (' 5159 c8edd286da0efccb7b72)]

CodePudding user response:

If the amount of data use distinct doesn't (work well)
  • Related