Mongodb - aggregation $push if conditional
问题 I am trying to aggregate a batch of documents. There are two fields in the documents I would like to $push. However, lets say they are "_id" and "A" fields, I only want $push "_id" and "A" if "A" is $gt 0. I tried two approaches. First one. db.collection.aggregate([{ "$group":{ "field": { "$push": { "$cond":[ {"$gt":["$A", 0]}, {"id": "$_id", "A":"$A"}, null ] } }, "secondField":{"$push":"$B"} }]) But this will push a null value to "field" and I don't want it. Second one. db.collection