MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Come faccio a filtrare per restituire un sottoinsieme di uno schema mongoose?

Puoi usare l'aggregazione

ChoreChart.aggregate([
  { "$match": { "affiliation": "liss_family" } },
  { "$addFields": {
    "chart": {
      "$map": {
        "input": "$chart",
        "as": "cc",
        "in": {
          "_id": "$$cc._id",
          "ordinal": "$$cc.ordinal",
          "chorePerson": {
            "$filter": {
              "input": "$$cc.chorePerson",
              "as": "dd",
              "cond": { "$eq": ["$$dd.personID", "5c6e3c74b9f5ed0016b00577"] }
            }
          }
        }
      }
    }
  }}
])