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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.