I’m performing MongoDB aggregation to create a new collection from an existing one, and I’m struggling to find a way to group elements by count, rather than by values.
I want to achieve something like this:
data:
[
{"_id": "my_id_0"},
{"_id": "my_id_1"},
{"_id": "another_id"},
{"_id": "another_id_123"},
{"_id": "_id"},
{"_id": "document_id"},
{"_id": "document_id_1"},
{"_id": "document_id_2"},
{"_id": "document_id_3"},
{"_id": "document_id_4"},
]
query
db.coll.aggregate([
{
$someNonExistingStage: {
output: {
chunk: {"$push": "$_id"}
},
n: 3
}
}
])
result:
[
{"chunk": ["my_id_0", "my_id_1", "another_id"]},
{"chunk": ["another_id_123", "_id", "document_id"]},
{"chunk": ["document_id_1", "document_id_2", "document_id_3"]},
{"chunk": ["document_id_4"]},
]
The real length of chunks I want to have is about more or less 1024
I think maybe it can be achieved using bucketAuto or setWindowFields, but it looks like I should enumerate all the documents first, which is not clear.
Thanks in advance.
2
Answers
https://mongoplayground.net/p/zNnIBweQBDf
The database isn’t really doing anything for you in this scenario. We are neither filtering nor grouping documents to reduce the amount of material pulled from the collection and transmitted to the client, and we are not exploiting indexes. We might as well just run a loop on the client side: