5

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.

0

1 Answer 1

7

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})
Sign up to request clarification or add additional context in comments.

3 Comments

thanks thanks, I tried something like field[0] :| was so close
Is there a way to query last index if I donot know the length of array?
@TayyabFerozi: have a look here stackoverflow.com/questions/28680295/…

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.