MongoDB
 sql >> Datenbank >  >> NoSQL >> MongoDB

Verwenden Sie Aggregate mit $group in mongodb

Sie müssen $lookup hier. Einfach $group mit $cond wird die Arbeit erledigen.

db.collection.aggregate([
  { "$group": {
    "_id": null,
    "billingHours": {
      "$sum": {
        "$cond": [{ "$eq": ["$isBilling", true] }, "$hours", 0]
      }
    },
    "fixContract": {
      "$sum": {
        "$cond": [{ "$eq": ["$isBilling", true] }, 0, "$hours"]
      }
    }
  }}
])