mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Feature request (maybe): Populate using pipeline

Open sandorvasas opened this issue 5 years ago • 3 comments

We have a multi-tenanted app (multiple teams), and as an extra layer of security to ensure data integrity, we'd like to ensure that when a reference is populated, it doesn't point to another team. I figured something like this could be done with virtual fields, if $lookup pipeline could be defined. In our case we have the team field defined for almost all documents, meaning in the pipeline I could just check that the referenced document's team field is the same as the current document's team field or not. Something like this:

UserSchema.virtual('contact', {
  justOne: true,
  from: 'contacts',
  as: 'contact',
  let: { 
     teamId: '$team',
     contactId: '$contact',
  },
  pipeline: [
     {$match: {
         _id: '$$contactId',
         team: '$$teamId'
     }}
  ]
})

Question: Is this supported currently, or are there plans to support it?

sandorvasas avatar Dec 17 '20 11:12 sandorvasas

We do not currently support this, we may add support in the future. Thanks for the suggestion!

vkarpov15 avatar Dec 19 '20 04:12 vkarpov15

@vkarpov15 Any plan on this new feature?

ghostoy avatar Apr 03 '23 07:04 ghostoy

Not currently. We're unlikely to prioritize this issue, because we don't want to use $lookup for populate(). Can you please describe your use case @ghostoy ?

vkarpov15 avatar Apr 04 '23 14:04 vkarpov15