How to $push a field depending on a condition?
问题 I'm trying to conditionally push a field into an array during the $group stage of the MongoDB aggregation pipeline. Essentially I have documents with the name of the user, and an array of the actions they performed. If I group the user actions like this: { $group: { _id: { "name": "$user.name" }, "actions": { $push: $action"} } } I get the following: [{ "_id": { "name": "Bob" }, "actions": ["add", "wait", "subtract"] }, { "_id": { "name": "Susan" }, "actions": ["add"] }, { "_id": { "name":