Same field in multiple conditions in match or find: they are treated as "OR"?

Looks like that when same field is used in multiple conditions in match or find, those sonditions are treated as joinned by logical “OR”, not “AND”:
match {
{‘responseMessage’: { $exists: true }},
{‘responseMessage’: {$ne: “No Suggestions Found”}}
}
returns all documents where responseMessage exists OR responseMessage is not equal to “No Suggestions Found”, which actually returns documents where responseMessage DOES NOT EXIST.
If I need to have those confitions to be joined with “AND”, I need to put them like:

{ $and: [
{‘responseMessage’: { $exists: true }},
{‘responseMessage’: {$ne: “No Suggestions Found”}}
]}

This is different from behavior when 2 conditions are using different fields - those conditions will be treated as joined by “AND”

Hi @SERGIY_MITIN. Welcome to the community.

Yes, here’s the documentation when specifying the same field. Your query can be rewritten with an implicit AND, like below

db.coll.find({ responseMessage: { $exists: true, $ne: "no suggestions found"}})

Hope this adds clarity.

  • Mahi

This is not what really happen.

The reason for that is that in most JSON implementations when a field name is repeated only the last occurrence is kept.

So in your case, responseMessage:{$exists:true} is not even seen by the server.

You may experiment in mongosh with :

query = {
    "responseMessage" : "any value" ,
    "responseMessage" : { "$exists" : true } ,
    "responseMessage" : { "$ne" : "No Suggestions Found" }
}

You will see that mongosh outputs:

{ responseMessage: { '$ne': 'No Suggestions Found' } }

showing that all other occurrences of responseMessage are ignored.