Filter array within collection when publishing


#1

I’m posting here because I’m completely stuck and I’m not sure what to do. Basically, I have a single collection that contains an array. This collection can be accessed by multiple users however I want to limit the data within the array from being published.

In the example below, the candidateApplications array contains candidateUserId for each user. If candidateUserId: 1 views the collection I want my publish function to only return:

Is it possible to do this?

{
  "_id": "7ARk3dc2JA8g5pamA",
  "jobTitle": "Developer",
  "candidateApplications": [
    {
      "candidateUserId": "1",
      "applied": true
    }
  ]
}

Eg: Collection

{
  "_id": "7ARk3dc2JA8g5pamA",
  "jobTitle": "Developer",
  "candidateApplications": [
    {
      "candidateUserId": "1",
      "applied": true
    },
    {
      "candidateUserId": "2",
      "applied": false
    }
  ]
}

Path: Publish command

return Jobs.find({ _id: jobCollectionId }, {
  fields: {
    candidateApplication: 0
  }
});

#2

I’ve read this from Stack overflow: https://stackoverflow.com/questions/30207005/how-to-filter-array-in-a-mongodb-query

Hope this helps.


#3

Can you use Mongo DB $where to query for candidate number?

https://docs.mongodb.com/manual/reference/operator/query/where/


#4

@globalise how do you use the $where in this case as this is inside an array?