Hello Team,
Below is my data set,
db.Test.insert({ "_id": 1, "Name": "A" });
db.Test.insert({ "_id": 2, "Name": "B" });
db.Test.insert({ "_id": 3, "Name": "C" });
db.Test.insert({ "_id": 4, "Name": "C" });
db.Test.insert({ "_id": 5, "Name": "D" });
db.Test.insert({ "_id": 6, "Name": "E" });
db.Test.insert({ "_id": 7, "Name": "F" });
db.Test.insert({ "_id": 8, "Name": "G" });
db.Test.insert({ "_id": 9, "Name": "H" });
db.Test.insert({ "_id": 10, "Name": "I" });
Aggregate Query
db.Test.aggregate( [
{
$bucketAuto: {
groupBy: "$_id",
buckets: 5,
output: {"Unique": {"$addToSet": "$_id"}},
}
}
] )
Results
{ "_id" : { "min" : 1, "max" : 3 }, "Unique" : [ 1, 2 ] }
{ "_id" : { "min" : 3, "max" : 5 }, "Unique" : [ 3, 4 ] }
{ "_id" : { "min" : 5, "max" : 7 }, "Unique" : [ 6, 5 ] }
{ "_id" : { "min" : 7, "max" : 9 }, "Unique" : [ 7, 8 ] }
{ "_id" : { "min" : 9, "max" : 10 }, "Unique" : [ 9, 10 ] }
How can I also add a new field and enable auto incrementing counter.
{ "_id" : { "min" : 1, "max" : 3 }, "Unique" : [ 1, 2 ] , "Counter": 1}
{ "_id" : { "min" : 3, "max" : 5 }, "Unique" : [ 3, 4 ], "Counter": 2 }
{ "_id" : { "min" : 5, "max" : 7 }, "Unique" : [ 6, 5 ],"Counter": 3 }
{ "_id" : { "min" : 7, "max" : 9 }, "Unique" : [ 7, 8 ] , "Counter": 4}
{ "_id" : { "min" : 9, "max" : 10 }, "Unique" : [ 9, 10 ] , "Counter": 5}
Basically every bucket should be assigned a counter with incrementing value. How can I achieve this mongo aggregate 4.4 ?
Regards,
Rama