MongoDB
 SQL >> Database >  >> NoSQL >> MongoDB

Raggruppare con più condizioni in aggregazione MongoDB

Dentro $cond si possono combinare più confronti con $and.

collection.aggregate({
  $group: {
    _id: '$someField',
    count: {
      $sum: {
        $cond: [
          {
            $and: [
              { $eq: ["$otherField", false] },
              { $eq: ["$anotherField", "value"] }
            ]
          },
          1,
          0
        ]
      }
    }
  }
});

Il conteggio aumenta solo quando entrambe le condizioni sono vere.