sql >> Base de Datos >  >> NoSQL >> MongoDB

Use Agregado con $ grupo en mongodb

No necesita usar $lookup aquí. Simple $group con $cond hará el trabajo.

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