The collection defined in this question is as follows:
db.collection.find()
{
“a”: [1, 34, 13]
}
Question 1 : Can you explain, how did you manage to get a collection without the _id field? The _id field should have been created by default, and since you haven’t used a projection to unset it, how come your output above is not printing _id field?
Question 2: Following aggregation pipeline has been provided.
Pipeline 1
db.collection.aggregate([
{"$match": { “a” : {"$sum": 1} }},
{"$project": { “_id” : {"$addToSet": “$a”} }},
{"$group": { “_id” : “”, “max_a”: {"max" : "_id"} }}
])
Here stage 2 is having a $addToSet, so my understanding is that it should transform the _id field into an array, by pushing in the value of $a (i.e an array with only 1 element). Moving to the next stage, i.e. Stage 3 - The $group will pick all documents from the collection and group them, since it is using a null string for grouping _id. It is using an accumulator of $max, on _id field from stage 2. Since _id from Stage 2 was transformed into an array filed, how does $max even work with arrays, finding a max array across a group of arrays?