Mongo DB aggregation with $project and $filter: $add and $subtract return null - mongodb

So I'm running a pretty big aggregation query in mongo shell (just for testing purpose)
in my last $project step, i use $filter to select a range of elements.
$filter: {
"input": "$users",
"as": "users",
"cond": {
$and: [
{
$lte: [
"$$users.ranking",
{$add: ["$myUser[0].ranking", 5]}
]
},
{
$gte: [
"$$users.ranking",
{$subtract: ["$myUser[0].ranking", 5]}
]
}
]
}
}
$subtract and $add both return null, any idea how i get it correct?
MongoVersion: 3.6.3, running in a docker container using the mongo 3.6.3 image.
Correct output should be:
"users" : [
{
"_id" : ObjectId("5ba3c2089a3a3e26a859f11b"),
"sgId" : ObjectId("5b76c1040c3aa5000559e6b3"),
"score" : 30,
"ranking" : NumberLong("0")
},
{
"_id" : ObjectId("5ba3c1d89a3a3e26a859f11a"),
"sgId" : ObjectId("5b76c1000c3aa500060e0fd2"),
"score" : 20,
"ranking" : NumberLong("1")
},
{
"_id" : ObjectId("5ba4fa3b71936b33e46569b9"),
"sgId" : ObjectId("5b76c8a3f7d606000566b652"),
"score" : 10,
"ranking" : NumberLong("2")
},
{
"_id" : ObjectId("5ba4fa4c71936b33e46569ba"),
"sgId" : ObjectId("5b76cafbf7d6060006270c90"),
"score" : 9,
"ranking" : NumberLong("3")
},
{
"_id" : ObjectId("5ba4fe6e71936b33e46569bb"),
"sgId" : ObjectId("5b7a4e69f7d606000566b65f"),
"score" : 8,
"ranking" : NumberLong("4")
},
{
"_id" : ObjectId("5ba4fe7471936b33e46569bc"),
"sgId" : ObjectId("5b7a4f47f7d6060006270cc4"),
"score" : 7,
"ranking" : NumberLong("5")
},
{
"_id" : ObjectId("5ba4fe8871936b33e46569bd"),
"sgId" : ObjectId("5b7a5265f7d606000566b67e"),
"score" : 6,
"ranking" : NumberLong("6")
}
]
Complete Query:
db.highscore.aggregate([
{
$sort: {score: -1}
},
{
$group: {
"_id": false,
"users": {
$push: {
"_id": "$_id",
"sgId": "$sgId",
"score": "$score",
}
}
}
},
{
$unwind: {
"path": "$users",
"includeArrayIndex": "ranking"
}
},
{
$group: {
"_id": false,
"users": {
$push: {
"_id": "$users._id",
"sgId": "$users.sgId",
"score": "$users.score",
"ranking": "$ranking"
}
}
}
},
{
$project: {
"users": "$users",
"myUser": {
$filter: {
"input": "$users",
"as": "user",
"cond": {
$eq: ["$$user.sgId", ObjectId("5b76c1000c3aa500060e0fd2")]
}
}
}
}
},
{
$project: {
"myUser": "$myUser",
"users" : {
$filter: {
"input": "$users",
"as": "users",
"cond": {
$and: [
{
$lte: [
"$$users.ranking",
{$add: ["$myUser[0].ranking", NumberLong("5")]}
]
},
{
$gte: [
"$$users.ranking",
{$subtract: ["$myUser[0].ranking", NumberLong("5")]}
]
}
]
}
}
}
}
},
])
Used Documents:
{
"_id" : ObjectId("5ba3c1d89a3a3e26a859f11a"),
"sgId" : ObjectId("5b76c1000c3aa500060e0fd2"),
"type" : "a",
"score" : 20,
"created" : ISODate("2018-09-20T17:50:48.024+02:00")
},
{
"_id" : ObjectId("5ba3c2089a3a3e26a859f11b"),
"sgId" : ObjectId("5b76c1040c3aa5000559e6b3"),
"type" : "a",
"score" : 30,
"created" : ISODate("2018-09-20T17:51:36.258+02:00")
},
{
"_id" : ObjectId("5ba4fa3b71936b33e46569b9"),
"sgId" : ObjectId("5b76c8a3f7d606000566b652"),
"type" : "a",
"score" : 10,
"created" : ISODate("2018-09-20T17:50:48.024+02:00")
},
{
"_id" : ObjectId("5ba4fa4c71936b33e46569ba"),
"sgId" : ObjectId("5b76cafbf7d6060006270c90"),
"type" : "a",
"score" : 9,
"created" : ISODate("2018-09-20T17:50:48.024+02:00")
}

Found it,
i just needed to add an $unwind before the last $project to convert the myUser Array into an object - then i was able to reach it for the add.
So full pipeline to get rankings of a highscore list and a range with your given user as source.
db.highscore.aggregate([
{
$sort: {score: -1}
},
{
$group: {
"_id": false,
"users": {
$push: {
"_id": "$_id",
"sgId": "$sgId",
"score": "$score",
}
}
}
},
{
$unwind: {
"path": "$users",
"includeArrayIndex": "ranking"
}
},
{
$group: {
"_id": false,
"users": {
$push: {
"_id": "$users._id",
"sgId": "$users.sgId",
"score": "$users.score",
"ranking": "$ranking"
}
}
}
},
{
$project: {
"users": "$users",
"myUser": {
$filter: {
"input": "$users",
"as": "user",
"cond": {
$eq: ["$$user.sgId", ObjectId("5b76c1000c3aa500060e0fd2")]
}
}
}
}
},
{
$unwind: {
path: '$myUser'
}
},
{
$project: {
"myUser": "$myUser",
"users" : {
$filter: {
"input": "$users",
"as": "users",
"cond": {
$and: [
{
$lte: [
"$$users.ranking",
{$add: ["$myUser.ranking", NumberLong("2")]}
]
},
{
$gte: [
"$$users.ranking",
{$subtract: ["$myUser.ranking", NumberLong("2")]}
]
}
]
}
}
}
}
},
], {'allowDiskUse': true})

Related

How to iterate list in mongodb $lookup and pipeline

I have two collections i.e parent and chilednodes.
{
"_id" : "5e6cd8c1996ddf1c28e14505",
"parentList" : [
{
"_id" : "5e6c70e8996ddf1c28e14504",
"startDate" : "2020-02-25T14:01:58.697Z",
"active_id" : "child_vesrion_1",
"child_id" : "5e5e2cd4e972a95b6c32b5bf30"
},
{
"_id" : "5e6c70e8996ddf1c28e14506",
"startDate" : "2020-02-25T14:01:58.697Z",
"active_id" : "child_vesrion_1",
"child_id" : "5e5e2cd4e972a95b6c32b5bf31"
}
]
}
And childnodes are;
{
"_id" : "5e5e2cd4e972a95b6c32b5bf31",
"startDate" : "2020-03-25T14:01:58.697Z",
"endDate" : null,
"child_vesrion_1" : {
"childName" : "test3",
"createdDate" : "2020-02-25T14:01:58.697Z",
"text" : "test3 text",
"type" : "test3 type"
},
"child_vesrion_2" : {
"childName" : "Test4",
"createdDate" : "2020-02-25T14:01:58.697Z",
"text" : "test4 text",
"type" : "test4 type"
},
"active" : "child_vesrion_1"
},
{
"_id" : "5e5e2cd4e972a95b6c32b5bf30",
"startDate" : "2020-02-25T14:01:58.697Z",
"endDate" : null,
"child_vesrion_1" : {
"childName" : "test1",
"createdDate" : "2020-02-25T14:01:58.697Z",
"text" : "test1 text",
"type" : "test1 type"
},
"child_vesrion_2" : {
"childName" : "test2",
"createdDate" : "2020-02-25T14:01:58.697Z",
"text" : "test2 text",
"type" : "test2 type"
},
"active" : "child_vesrion_1"
}
Here is my query;
db.parent.aggregate([
{ $match: { "_id": "5e6cd8c1996ddf1c28e14505" } },
{
$lookup: {
from: "childnodes",
let: { "child_id": "$parentList.child_id", "activeid": "$parentList.active_id" },
pipeline: [
{ $match: { "$expr": { $eq: ["$_id", "$$child_id"] } } },
{
$project: {
"child_id": "$_id",
"start_date": "$startDate",
"current_version_Key": "$active",
"active_child_name": {
"$reduce": {
"input": { "$objectToArray": "$$ROOT" },
"initialValue": "",
"in": {
"$cond": [{ "$eq": ["$$this.k", "$$activeid"] },
"$$this.v.childName",
"$$value"
]
}
}
},
"text": {
"$reduce": {
"input": { "$objectToArray": "$$ROOT" },
"initialValue": "",
"in": {
"$cond": [{ "$eq": ["$$this.k", "$$activeid"] },
"$$this.v.text",
"$$value"
]
}
}
},
"type": {
"$reduce": {
"input": { "$objectToArray": "$$ROOT" },
"initialValue": "",
"in": {
"$cond": [{ "$eq": ["$$this.k", "$$activeid"] },
"$$this.v.type",
"$$value"
]
}
}
}
}
}
],
as: "finalList",
},
},
{
$project: {
parentList: 0,
},
},
]);
I am expecting results like;
{
"_id": "5e6cd8c1996ddf1c28e14505",
"finalList": [
{
"child_id": "5e5e2cd4e972a95b6c32b5bf30",
"start_date": "2020-02-25T14:01:58.697Z",
"current_version_Key": "child_vesrion_1",
"active_child_name": "test1",
"text": "test1 text",
"type": "test1 type",
},
{
"child_id": "5e5e2cd4e972a95b6c32b5bf31",
"start_date": "2020-02-25T14:01:58.697Z",
"current_version_Key": "child_vesrion_1",
"active_child_name": "test3",
"text": "test3 text",
"type": "test3 type",
}
]
}
But i am not getting anything in finalList. It is returning an empty array.
I have tried with different approaches but it didn't help me. I am bit new to mongodb, any help on this would be appreciable.
You were so close. Your parentList is an array, so when you define child_id and activeid inside $lookup, they are also array.
If we add $unwind before the $lookup + $group at the end, your query works as expected.
Try this one:
db.parent.aggregate([
{
$match: {
"_id": "5e6cd8c1996ddf1c28e14505"
}
},
{
$unwind: "$parentList"
},
{
$lookup: {
from: "childnodes",
let: {
"child_id": "$parentList.child_id",
"activeid": "$parentList.active_id"
},
pipeline: [
{
$match: {
"$expr": {
$eq: [
"$_id",
"$$child_id"
]
}
}
},
{
$addFields: {
child_version: {
$arrayElemAt: [
{
$filter: {
input: {
$objectToArray: "$$ROOT"
},
cond: {
$eq: [
"$$this.k",
"$$activeid"
]
}
}
},
0
]
}
}
},
{
$project: {
"_id": 0,
"child_id": "$_id",
"start_date": "$startDate",
"current_version_Key": "$active",
"active_child_name": "$child_version.v.childName",
"text": "$child_version.v.text",
"type": "$child_version.v.type"
}
}
],
as: "finalList"
}
},
{
$unwind: "$finalList"
},
{
$group: {
_id: "$_id",
parentList: {
$push: "$finalList"
}
}
}
])
MongoPlayground

How to push all values in single array in mongodb

Colleges
/* 1 createdAt:5/9/2019, 7:00:04 PM*/
{
"_id" : ObjectId("5cd42b5c65b41027845938ae"),
"clgID" : "100",
"name" : "Anna University"
},
/* 2 createdAt:5/9/2019, 7:00:04 PM*/
{
"_id" : ObjectId("5cd42b5c65b41027845938ad"),
"clgID" : "200",
"name" : "National"
}
Subjects:
/* 1 createdAt:5/9/2019, 7:03:24 PM*/
{
"_id" : ObjectId("5cd42c2465b41027845938b0"),
"name" : "Hindi",
"members" : {
"student" : [
"123"
]
},
"college" : {
"collegeID" : "100"
}
},
/* 2 createdAt:5/9/2019, 7:03:24 PM*/
{
"_id" : ObjectId("5cd42c2465b41027845938af"),
"name" : "English",
"members" : {
"student" : [
"456",
"789"
]
},
"college" : {
"collegeID" : "100"
}
}
Here i am having two collection and i want to join Colleges table is clgID and Subjects table iscollege.collegeID , then i want to take members.student values and push into single array based on college.collegeID.
My Expected Output
{
"GroupDetails" : [ ],
"clgName" : "National"
},
{
"GroupDetails" : [
"123",
"456",
"789"
],
"clgName" : "Anna University"
}
My Code
db.Colleges.aggregate([
{ $match : { "clgID" : { $in : ["100", "200"] } } },
{ $lookup: { from: "Subjects", localField: "clgID", foreignField: "college.collegeID", as: "GroupDetails" } },
//{ $unwind: "$GroupDetails" },
{ $project: { '_id' : false, 'clgName' : '$name', 'GroupDetails.members.student' : true } }
])
I am getting like this
/* 1 */
{
"GroupDetails" : [ ],
"clgName" : "National"
},
/* 2 */
{
"GroupDetails" : [
{
"members" : {
"student" : [
"456"
]
}
},
{
"members" : {
"student" : [
"123"
]
}
}
],
"clgName" : "Anna University"
}
You can use below aggregation with mongodb 3.6 and above
db.Colleges.aggregate([
{ "$match": { "clgID": { "$in": ["100", "200"] } } },
{ "$lookup": {
"from": "Subjects",
"let": { "clgId": "$clgID" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$$clgId", "$college.collegeID"] } } },
{ "$group": {
"_id": "$college.collegeID",
"groupDetails": { "$push": "$members.student" }
}},
{ "$project": {
"groupDetails": {
"$reduce": {
"input": "$groupDetails",
"initialValue": [],
"in": { "$concatArrays": ["$$this", "$$value"] }
}
}
}}
],
"as": "clg"
}},
{ "$unwind": { "path": "$clg", "preserveNullAndEmptyArrays": true } },
{ "$project": {
"clgName": "$name",
"groupDetails": { "$ifNull": ["$clg.groupDetails", []] }
}}
])
MongoPlayground
Or with the mongodb 3.4 and below
db.Colleges.aggregate([
{ "$match": { "clgID": { "$in": ["100", "200"] }}},
{ "$lookup": {
"from": "Subjects",
"localField": "clgID",
"foreignField": "college.collegeID",
"as": "clg"
}},
{ "$unwind": { "path": "$clg", "preserveNullAndEmptyArrays": true }},
{ "$group": {
"_id": { "clgId": "$clg.college.collegeID", "_id": "$_id" },
"groupDetails": { "$push": "$clg.members.student" },
"clgName": { "$first": "$name" }
}},
{ "$project": {
"_id": "$_id._id",
"clgName": 1,
"groupDetails": {
"$reduce": {
"input": "$groupDetails",
"initialValue": [],
"in": { "$concatArrays": ["$$this", "$$value"] }
}
}
}}
])
MongoPlayground

How to add two collections with single aggregation

I am new to MongoDb and would appreciate some help with this query. I wrote the following aggregation pipeline. I wrote the query from collection1 I got the output ("Conventional Energy" : 0.0036) and I wrote the query collection2 I got the output (LastMonthConsumption" : 2.08) but how to add two collection with single aggregation with(LastMonthConsumption" : 2.08 * Conventional Energy" : 0.0036/Conventional Energy" : 0.0036) this is my required output
I have this data in mongodb:
COLLECTION 1:DATA
{
"slcId" : "51",
"clientId" : "1",
"dcuId" : "1",
"type" : "L",
"officeId" : "200-24",
"lampStatus" : "OFF",
"cummulativeKWH" : 133.7,
"powerFactor" : 1.0,
"createDate" : ISODate("2018-09-06T00:01:34.816Z")
},
{
"slcId" : "52",
"clientId" : "1",
"dcuId" : "1",
"type" : "L",
"officeId" : "200-24",
"lampStatus" : "OFF",
"cummulativeKWH" : 133.7,
"powerFactor" : 1.0,
"createDate" : ISODate("2018-09-07T21:01:34.816Z")
}
COLLECTION2:DATA
{
"_class" : "MongoStreetLightMonthlyVo",
"timeId" : ISODate("2018-08-04T16:40:08.817Z"),
"vendor" : "CIMCON",
"slcId" : "123450",
"mongoStreetLightChildVo" : {
"totalConsumptionMtd" : 2.08,
"prevConsumptionMtd" : 3.45,
"perChargeKWH" : 9.85,
}
},
{
"_class" : "MongoStreetLightMonthlyVo",
"timeId" : ISODate("2018-09-04T16:40:08.817Z"),
"vendor" : "CIMCON",
"slcId" : "123450",
"mongoStreetLightChildVo" : {
"totalConsumptionMtd" : 2.08,
"prevConsumptionMtd" : 3.45,
"perChargeKWH" : 9.85,
}
}
Collection1:
db.collection1.aggregate([
{ $match:{"type" : "L"}},
{
$count: "TOTAL_Lights"
},
{ "$project": {
"Conventional Energy": {
"$divide": [
{ "$multiply": [
{ "$multiply": [ "$TOTAL_Lights" ,0.12 ] },
]},
1000
]
}
}},
])
output: {"Conventional Energy" : 0.0036}
Collection2:
db.collection2.aggregate(
[
// Stage 1
{
$group: {
_id:{year:{$year:"$timeId"},month:{$month:"$timeId"} },
LastMonthConsumption : {$sum:"$mongoStreetLightChildVo.totalConsumptionMtd"},
}
},
{
$redact: {
$cond: { if: { $and:[
{$eq: [ "$_id.year", {$year:new Date()} ]},
{$eq: [-1, {$subtract:[ "$_id.month", {$month:new Date()} ]}]}
]},
then: "$$KEEP",
else: "$$PRUNE"
}
}
},
{$project:{
_id:0,
LastMonthConsumption :1
}
}
]
);
output:{
"LastMonthConsumption" : 2.08
}
Expected output:
LastMonthConsumption - Conventional Energy/Conventional Energy*100
You can try below aggregation
db.collection2.aggregate([
{ "$group": {
"_id": { "year": { "$year": "$timeId" }, "month": { "$month": "$timeId" }},
"LastMonthConsumption": { "$sum": "$mongoStreetLightChildVo.totalConsumptionMtd" }
}},
{ "$redact": {
"$cond": {
"if": {
"$and": [
{ "$eq": ["$_id.year", { "$year": new Date() }] },
{ "$eq": [-1, { "$subtract": ["$_id.month", { "$month": new Date() }] }]
}
]
},
"then": "$$KEEP",
"else": "$$PRUNE"
}
}},
{ "$lookup": {
"from": "collection1",
"pipeline": [
{ "$match": { "type": "L" } },
{ "$count": "TOTAL_Lights" },
{ "$project": {
"ConventionalEnergy": {
"$divide": [{ "$multiply": [{ "$multiply": ["$TOTAL_Lights", 0.12] }] }, 1000]
}
}}
],
"as": "ConventionalEnergy"
}},
{ "$project": {
"_id": 0,
"totalConsumption": {
"$multiply": [
{
"$divide": [
{
"$subtract": [
"$LastMonthConsumption",
{ "$arrayElemAt": ["$ConventionalEnergy.ConventionalEnergy", 0] }
]
},
{ "$arrayElemAt": ["$ConventionalEnergy.ConventionalEnergy", 0] }
]
},
100
]
}
}}
])

Rewind data of two nested array field after $unwind and $lookup and $filter on date range in $project

{
"_id" : ObjectId("590b12b6330e1567acd29e69"),
"name": "Foo",
"sales_history" : [
{
"_id" : ObjectId("593ce8e4cfaa652df543d9e3"),
"sold_at" : ISODate("2017-06-11T06:53:24.881Z"),
"sold_to" : ObjectId("593509e938792e046ba14a02"),
"sold_products" : [
{
"product_dp" : 100,
"quantity" : 1,
"product_id" : ObjectId("591068be1f4c6c79a442a788"),
"_id" : ObjectId("593ce8e4cfaa652df543d9e5")
},
{
"product_dp" : 100,
"quantity" : 1,
"product_id" : ObjectId("593a33dccfaa652df543d924"),
"_id" : ObjectId("593ce8e4cfaa652df543d9e4")
}
]
},
{
"_id" : ObjectId("5944cb7142a04740357020b9"),
"sold_at" : ISODate("2017-06-17T06:25:53.332Z"),
"sold_to" : ObjectId("5927d4a59e58ba0c61066f3b"),
"sold_products" : [
{
"product_dp" : 500,
"quantity" : 1,
"price" : 5650,
"product_id" : ObjectId("593191ed53a2741dd9bffeb5"),
"_id" : ObjectId("5944cb7142a04740357020ba")
}
]
}
]
}
I have User schema like this. I want detail of product_id reference, with a date range search criteria on sold_at date field.
My expected data like following when I searched in sold_at at: 2017-06-11
{
"_id" : ObjectId("590b12b6330e1567acd29e69"),
"name": "Foo",
"sales_history" : [
{
"_id" : ObjectId("593ce8e4cfaa652df543d9e3"),
"sold_at" : ISODate("2017-06-11T06:53:24.881Z"),
"sold_to" : ObjectId("593509e938792e046ba14a02"),
"sold_products" : [
{
"product_dp" : 100,
"quantity" : 1,
"product_id": {
_id:ObjectId("hsfgg123412yh3gy1u2g3"),
name: "Product1",
code: "FG0154"
},
}
]
}
]
}
Product detail need to be populate in product_id, sales_history array need to be filtered in date range.
You can try below aggregation query.
$filter sales history on date range followed by $unwinding sales history & sold_products.
$lookup sold_products to get the product details.
$group back sold_products & sales history
db.collection.aggregate([
{
"$project": {
"name": 1,
"sales_history": {
"$filter": {
"input": "$sales_history",
"as": "history",
"cond": {
"$and": [
{
"$gte": [
"$$history.sold_at",
ISODate("2017-06-11T00:00:00.000Z")
]
},
{
"$lt": [
"$$history.sold_at",
ISODate("2017-06-12T00:00:00.000Z")
]
}
]
}
}
}
}
},
{
"$unwind": "$sales_history"
},
{
"$unwind": "$sales_history.sold_products"
},
{
"$lookup": {
"from": lookupcollection,
"localField": "sales_history.sold_products.product_id",
"foreignField": "_id",
"as": "sales_history.sold_products.product_id"
}
},
{
"$group": {
"_id": {
"_id": "$_id",
"sales_history_id": "$sales_history._id"
},
"name": {
"$first": "$name"
},
"sold_at": {
"$first": "$sales_history.sold_at"
},
"sold_to": {
"$first": "$sales_history.sold_to"
},
"sold_products": {
"$push": "$sales_history.sold_products"
}
}
},
{
"$group": {
"_id": "$_id._id",
"name": {
"$first": "$name"
},
"sales_history": {
"$push": {
"_id": "$_id.sales_history_id",
"sold_at": "$sold_at",
"sold_to": "$sold_to",
"sold_products": "$sold_products"
}
}
}
}
]);

How could I not only preserve the max value but also the record having the max value

I want to use $max operator to select the max value.
And also keep the max record with the key "original_document"
How could I do it in mongoDB
expect result
{ "_id" : "abc", "maxTotalAmount" : 100,
"maxQuantity" : 10,
"original_document": {{ "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-15T08:00:00Z") }}}
current result
{ "_id" : "abc", "maxTotalAmount" : 100, "maxQuantity" : 10 }
documents
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:00:00Z") }
{ "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-15T08:00:00Z") }
aggregation
db.sales.aggregate(
[
{
$group:
{
_id: "$item",
maxTotalAmount: { $max: { $multiply: [ "$price", "$quantity" ] } },
maxQuantity: { $max: "$quantity" }
}
}
]
)
When you want detail from the same grouping item then you use $sort and $first for the field(s) from the document you wish to preserve:
db.sales.aggregate([
{ "$project": {
"item": 1,
"TotalAmount": { "$multiply": [ "$price", "$quantity" ] },
"quantity": 1
}},
{ "$sort": { "TotalAmount": -1 } },
{ "$group": {
"_id": "$item",
"maxTotalAmount": { "$max": "$TotalAmount" },
"maxQuantity": { "$max": "$quantity" },
"doc_id": { "$first": "$_id" },
"doc_quantity": { "$first": "$quantity" }
}}
])
The aggregation "accumulators" cannot use embedded fields, and pushing all to an array makes little sense. But you can name like above and even rename with another $project or in your code if you want to.
Just to demonstrate how impractical this is to do otherwise, there is this example:
db.sales.aggregate([
{ "$group": {
"_id": "$item",
"maxTotalAmount": { "$max": { "$multiply": [ "$price", "$quantity" ] } },
"maxQuantity": { "$max": "$quantity" },
"docs": { "$push": {
"_id": "$_id",
"quantity": "$quantity",
"TotalAmount": { "$multiply": [ "$price", "$quantity" ] }
}}
}},
{ "$project": {
"maxTotalAmount": 1,
"maxQuantity": 1,
"maxTotalDocs": {
"$setDifference": [
{ "$map": {
"input": "$docs",
"as": "doc",
"in": {
"$cond": [
{ "$eq": [ "$maxTotalAmount", "$$doc.TotalAmount" ] },
"$$doc",
false
]
}
}},
[false]
]
}
}}
])
Which is not a great idea since you are pushing every document within the grouping condition into an array, only to filter out the ones you want later. On any reasaonable data size this is not practical and likely to break.
Please check the below :
db.qt.aggregate([
{ "$project": { "maxTotalAmount" : { "$multiply" :
[ "$price", "$quantity" ]
} ,
"currentDocumnet" : { "_id" : "$_id" ,
"item" : "$item", "price" : "$price",
"quantity" : "$quantity",
"date" : "$date" } }
},
{"$sort" : { "currentDocumnet.item" : 1 , maxTotalAmount : -1}},
{"$group" :{ _id : "$currentDocumnet.item" ,
currentDocumnet : { "$first" : "$currentDocumnet"} ,
maxTotalAmount : { "$first" : "$maxTotalAmount"} ,
maxQuantity: { "$max" : "$currentDocumnet.quantity" }}
}
]);