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

Contare notifiche non lette in proiezione MongoDB

Usa $map per scorrere le sottostanze e $filter con $size per contare le non lette.

db.collection.find(
  { id: 1 },
  { _id: 0, notRead: { $size: { $filter: {
    input: "$notifications",
    cond: { $not: { $in: ["User1", "$$this.read"] } }
  } } } }
)

Espressioni di aggregazione in find() richiedono MongoDB 4.4+.