4

For example if I have below as my documents

{
    "field": [
        "hello",
        "random wording",
        {
            "otherId": 3232,
            "otherId2": 32332
        }
    ],
}

is it possible to let's say have a query that matches the index 0 and index 2

I tried having query such as model.find({field: "hello") which seem to work to query it.

But if I want to mix and match, let's say I want to make a query that matches whatever index 0 and index 2 or let's say in index 2 the value is actually an object that I want to query all with "otherId2": 32332, instead that I need to match the whole object

Thanks in advance for any help or advises.

Dora
  • 6,776
  • 14
  • 51
  • 99

1 Answers1

6

Query for a value exactly at an index 0 of array

model.find({'field.0': 'hello'})

Referencing array indexes works for the first-level array only.

Query by object property present at index 2 of array

model.find({'field.2.otherId': 3232})
cheekujha
  • 781
  • 4
  • 12