user11553898
user11553898

Reputation:

How to remove results that do not meet the filter criteria in MongoDB aggregation pipeline?

I am using aggregation over two collections - one of which is nested. The collection looks like this:

[
  {
    "_id": "5fe72f0b4fd2c131bcc7dae0",
    "sirname": "Smith",
    "district": "Texas",
    "events": [
      {
        "_id": "5fe73e91ede45b3d2eca504c",
        "group": "Dogs"
      }
    ]
  },
  {
    "_id": "5fe72f0b4fd2c131bcc7dadf",
    "sirname": "Johnson",
    "district": "Washington",
    "events": [
      {
        "_id": "5fe73e91ede45b3d2eca504b",
        "group": "Cats"
      }
    ]
  },
  {
    "_id": "5fe72f0b4fd2c131bcc7dade",
    "sirname": "MacGruber",
    "district": "Washington",
    "events": [
      {
        "_id": "5fe73e91ede45b3d2eca504a",
        "group": "Dogs"
      }
    ]
  },
  {
    "_id": "5fe72f0b4fd2c131bcc7dadd",
    "sirname": "Plissken",
    "district": "Washington",
    "events": [
      {
        "_id": "5fe73e91ede45b3d2eca5049",
        "group": "Cats"
      }
    ]
  },
  {
    "_id": "5fe72f0b4fd2c131bcc7dadc",
    "sirname": "Williams",
    "district": "Texas",
    "events": [
      {
        "_id": "5fe73e91ede45b3d2eca5048",
        "group": "Dogs"
      }
    ]
  }
]

Here is the query code:

db.collection.aggregate([
  {
    "$match": {
      district: "Washington"
    }
  },
  {
    $project: {
      sirname: 1,
      district: 1,
      events: {
        "$filter": {
          input: "$events",
          as: "event",
          cond: {
            "$eq": [
              "$$event.group",
              "Cats"
            ]
          }
        }
      }
    }
  }
])

The result is an array like this:

[
  {
    "_id": "5fe72f0b4fd2c131bcc7dadf",
    "district": "Washington",
    "events": [
      {
        "_id": "5fe73e91ede45b3d2eca504b",
        "group": "Cats"
      }
    ],
    "sirname": "Johnson"
  },
  {
    "_id": "5fe72f0b4fd2c131bcc7dade",
    "district": "Washington",
    "events": [],
    "sirname": "MacGruber"
  },
  {
    "_id": "5fe72f0b4fd2c131bcc7dadd",
    "district": "Washington",
    "events": [
      {
        "_id": "5fe73e91ede45b3d2eca5049",
        "group": "Cats"
      }
    ],
    "sirname": "Plissken"
  }
]

My question is how to exclude from the result the object that does not meet the $filter conditions. That is, I want a result like this:

[
  {
    "_id": "5fe72f0b4fd2c131bcc7dadf",
    "district": "Washington",
    "events": [
      {
        "_id": "5fe73e91ede45b3d2eca504b",
        "group": "Cats"
      }
    ],
    "sirname": "Johnson"
  },
  {
    "_id": "5fe72f0b4fd2c131bcc7dadd",
    "district": "Washington",
    "events": [
      {
        "_id": "5fe73e91ede45b3d2eca5049",
        "group": "Cats"
      }
    ],
    "sirname": "Plissken"
  }
]

Also, i shared the code at mongo playground.

Regards!

Upvotes: 0

Views: 46

Answers (1)

sakura
sakura

Reputation: 34

you can test with this:

app.get('/filter', async (req, res) => {
    try {
        const result = await db.find({ "events.group": "Cats"})
        res.json(result);

    } catch (error) {
        console.log('Error occured: ', error);
        res.status(500).send(error)
    }
})

Upvotes: 0

Related Questions