Reputation: 879
I am using aggregation in MongoDB and now I am facing a problem, I want to project my fields based on a condition matching or not.
For example I have a field coupon_type
for which I will check if its value is equal to 1 then I will project fields ["curr_ctr", "total_coupons"]
otherwise if its value is not equal to 1 then i will project fields ["curr_ctr", "total_coupons", "curr_ctr", "coupons"].
I can use two queries and run them in parallel but I am trying to achieve my result using one single query.
Can anyone please tell me how can I do this in one single query?
UPDATE
My documents are like below
[{ "_id" : ObjectId("5878e1edf1df5a2b69bcf60e"), "curr_ctr": 12, "total_coupons":35, "coupons": ["hello", "hello2"], "coupon_type" : 1 } ,
{ "_id" : ObjectId("5878e1eff1df5a2b69bcf60f"), "curr_ctr": 12, "total_coupons":35, "coupons": ["hello", "hello2"], "coupon_type" : 0 } ,
{ "_id" : ObjectId("5878e1f1f1df5a2b69bcf610"), "curr_ctr": 12, "total_coupons":35, "coupons": ["hello", "hello2"], "coupon_type" : 1 } ,
{ "_id" : ObjectId("5878e1f3f1df5a2b69bcf611"), "curr_ctr": 12, "total_coupons":35, "coupons": ["hello", "hello2"], "coupon_type" : 1 } ,
{ "_id" : ObjectId("5878e1f5f1df5a2b69bcf612"), "curr_ctr": 12, "total_coupons":35, "coupons": ["hello", "hello2"], "coupon_type" : 11 } ,
{ "_id" : ObjectId("5878e1f7f1df5a2b69bcf613"), "curr_ctr": 12, "total_coupons":35, "coupons": ["hello", "hello2"], "coupon_type" : 110 },
{ "_id" : ObjectId("5878e1f9f1df5a2b69bcf614"), "curr_ctr": 12, "total_coupons":35, "coupons": ["hello", "hello2"], "coupon_type" : 0 } ]
Now for all coupon_type equal to 0 i want to project fields ["curr_ctr", "total_coupons", "curr_ctr", "coupons"]
and for all coupon_type not equal to 0 i want to project fields ["curr_ctr", "total_coupons"]
UPDATE
Actually i want to project coupons
array from index n to n+1, where n is the input by the user.
Upvotes: 4
Views: 6790
Reputation: 7840
Let consider your collection contain following documents
[{ "_id" : ObjectId("5878e1edf1df5a2b69bcf60e"), "coupon_type" : 1 } ,
{ "_id" : ObjectId("5878e1eff1df5a2b69bcf60f"), "coupon_type" : 0 } ,
{ "_id" : ObjectId("5878e1f1f1df5a2b69bcf610"), "coupon_type" : 1 } ,
{ "_id" : ObjectId("5878e1f3f1df5a2b69bcf611"), "coupon_type" : 1 } ,
{ "_id" : ObjectId("5878e1f5f1df5a2b69bcf612"), "coupon_type" : 11 } ,
{ "_id" : ObjectId("5878e1f7f1df5a2b69bcf613"), "coupon_type" : 110 },
{ "_id" : ObjectId("5878e1f9f1df5a2b69bcf614"), "coupon_type" : 0 } ]
Now you need to use $eq
in project
as :
db.collectoin.aggregate({
"$project": {
"result": {
"$cond": {
"if": {
"$eq": ["$coupon_type", 1]
},
"then": ["curr_ctr", "total_coupons"],
"else": ["curr_ctr", "total_coupons", "curr_ctr", "coupons"]
}
}
}
})
As per new Update you should modify query as like this :
db.collection.aggregate({
"$project": {
"result": {
"$cond": {
"if": {
"$eq": ["$coupon_type", 1]
},
"then": ["$curr_ctr", "$total_coupons", "$coupons"],
"else": ["$curr_ctr", "$total_coupons"]
}
}
}
})
UPDATE
As per new update let consider your n
given by user for ex. : var n = 1
;
now query will be as below :
db.coupon.aggregate({
"$project": {
"result": {
"$cond": {
"if": {
"$eq": ["$coupon_type", 1]
},
"then": ["$curr_ctr", "$total_coupons", {
"coupons": {
"$slice": ["$coupons", n, n + 1]
}
}],
"else": ["$curr_ctr", "$total_coupons"]
}
}
}
})
Upvotes: 3