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.