2

so I'm working with a system that can only take the query-argument of a .find() query as input, which means that I can not input an iterative script or an aggregation query.

However, I need to be able to find all documents that contain in one of their array fields the value that is set in another field. So, if my documents look as follows:

    { _id: ..., FieldA: x, FieldB: [x, y, z] }

Then I want to find all documents that contain the value of FieldA in FieldB, in this example contain x in FieldB.

Is that at all possible?

ray
  • 11,310
  • 7
  • 18
  • 42
NewJob
  • 59
  • 3

1 Answers1

2

You can use $in with $expr for your case.

db.collection.find({
  $expr: {
    "$in": [
      "$FieldA",
      "$FieldB"
    ]
  }
})

Here is the Mongo playground for your reference.

ray
  • 11,310
  • 7
  • 18
  • 42