0

I have a collection with a (multi-level) nested array like this:

{ _id: "1", metadata: { parent: { tags: ['foo', 'bar'] } } }
{ _id: "2", metadata: { parent: { tags: ['foo'] } } }
{ _id: "3", metadata: { parent: { tags: ['bar'] } } }

I would like to query records matching parent's tags containing keywords like foo:

{ _id: "1", metadata: { parent: { tags: ['foo', 'bar'] } } }
{ _id: "2", metadata: { parent: { tags: ['foo'] } } }

How to write MongoDB query?

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
Mike
  • 3
  • 2

1 Answers1

1

Achieve with dot notation.

db.collection.find({
  "metadata.parent.tags": "foo"
})

Sample Mongo Playground

Yong Shun
  • 35,286
  • 4
  • 24
  • 46