MongoDB
 SQL >> Datenbank >  >> NoSQL >> MongoDB

Wie füge ich ein Feld abhängig von einer Bedingung per $push hinzu?

Du brauchst eine vorgeschaltete $match-Stufe in deiner Pipeline, um nur diejenigen Dokumente auszuwählen, bei denen „action“ ungleich „wait“ ist.

db.collection.aggregate([
  { "$match": { "action": { "$ne": "wait" } } },
  { "$group": {
      "_id": "$user.name",
      "actions": { "$push": "$action" },
      "total": { "$sum": 1 }
  } }
])