'mongodb command very slow

I have 3 documents like this:

{
_id: ObjectId("..."),
_details: {
    _session: ObjectId("example_1"),
},
{
_id: ObjectId("..."),
_details: {
    _session: ObjectId("example_1"),
},
{
_id: ObjectId("..."),
_details: {
    _session: ObjectId("example_2"),
}

And I'm trying to retrieve and group the _details._session's ids. Expected output for the above example dataset would be:

['example_1', 'example_2']

I have tried the following Python script:

cursor = mycol.find({}, {"_details.session": 1})
sessions = []
for doc in cursor:
    if doc['_details']['_session'] not in sessions:
        sessions.append(doc['_details']['_session'])

Problem is that it takes around 1 minute for 500 documents.

Is there any way to speed up that command? I need it to run the fastest way possible.



Solution 1:[1]

playground

db.collection.aggregate([
  {
    "$group": {
      "_id": null,
      "uniqueSessions": {
        "$addToSet": "$_details._session"
      }
    }
  }
])

You don't need to iterate through each document. You can achieve many things easily and efficiently using mongo aggregation framework.

You can add a $project stage to avoid _id:null in the output if it really bothers.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gibbs