0

I have one collection of documents that are formatted as follows:

{
  _id: ObjectId('617d318eb863ee273c66b111'),
  my_object: {
    _id: ObjectId('617d318eb863ee273c66b222')
    //other fields
  },
  exclusion_object: {
    my_arr: ObjectId('617d318eb863ee273c66b222')
    //other fields
  }
}

So I need to exclude the documents where my_object._id is included in exclusion_object.my_arr

I tried (in $match stage):

{
  $match:{
    'exclusion_object.my_arr': { $nin:['my_object._id','$exclusion_object.my_arr']}
  }
}

This doesn't seem to work.. any suggestion?

0

1 Answer 1

2

Query

  • $in query operator needs value so we cant use it with $fieldName
    { field: { $in: [<value1>, <value2>, ... <valueN> ] } }
  • $in aggregate operator takes expression also so we can use it with $fieldName
    { $in: [ <expression>, <array expression> ] }
  • when aggregate operator is used on $match $expr operator is needed also

Test code here

aggregate(
[{"$match": 
   {"$expr": 
     {"$not": [{"$in": ["$my_object._id", "$exclusion_object.my_arr"]}]}}}])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.