I'm a little confused

I wonder if the first match in “find()” is ignored.
only second match?

When changing gt and lt, only the "one behind ’ seems to be applied.

Hi @DEV_JUNGLE,

The issue is that you have duplicate field names at the same level in your query document, so only the last value ends up being set in a JavaScript object.

You can see the query that is actually being sent by creating a variable in the MongoDB shell:

> query = { test: { $lt: 3 }, test: { $gt: 3 } }
{ test: { '$gt': 3 } }

For a range query you can fix this by including multiple query conditions in the same subdocument instead of duplicating field criteria:

db.korea.find({test: { $gt:1, $lt: 5 }})

For cases where you need to repeat the same field or operator at the same level, you can use the $and operator.

See Request for explanation! -- Chapter 4: Query Operators Lecture - #12 by Stennie for more background.

Since you are searching documents with arrays, it would also be worth reviewing the documentation to Query an Array as there are some helpful examples of queries such as matching one or more elements of an array.

Regards,
Stennie

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.