Any method to apply two filters to the same field without repeating the key?

In a situation such as below,

db.movieDetails.count({$and : {"tomato.consensus" : null, "tomato.consensus" : {$exists : true}}})

where I need to apply two filters to the same field, as an equality check for null returns all documents that have a ‘null’ value and don’t have that field present in the document at all. Can I combine both filters without having to rewrite the key for the field again?

Hey @Aditya_Dobriyal_85801

After looking at the docs for $and, I believe you can do this; see below.

Consider the below example:
db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )

This query can be also be constructed with an implicit AND operation by combining the operator expressions for the price field. For example, this query can be written as:
db.inventory.find( { price: { $ne: 1.99, $exists: true } } )

Hope this helps :slight_smile:

1 Like

This method works but only if both the values are using operators, in cases where I need to query the same field for containing a certain string or integer and using an operator on it as well, such as the situation in my question, where I need to know if the value is null (not a string in this case) and if it exists using the $exists operator, implicit AND does not work. Any path over this issue?

Could $type help in this case? I have not tested this but from what you said about both having to be operators. Where:

db.collection.find({ 'tomato.consensus' : { $type: 'null', $exists: 1 } })
# OR
db.collection.find({ 'tomato.consensus' : { $type: 10, $exists: 1 } })

Also coming to mind is $not. Which could be used to say: not of type string

Something like;??

db.collection.find({ 'tomato.consensus' : { $not: { $type: 'string' }, $exists: 1 } })
1 Like

@Aditya_Dobriyal_85801, I see no reason why what @natac13 suggested 2 days ago with $and or the short-cut would not work. If you look at https://docs.mongodb.com/manual/reference/operator/query/exists/ there is example about the short-cut where the field name is not repeated. Could you please come up with an example where it does not work so that we understand better what you try to do?

1 Like

Hi @Aditya_Dobriyal_85801,

In the above query, you have used the $and operator incorrectly. We always pass an array value to the $and operator which contains one or more expressions. For example :

db.movieDetails.count({$and : [{“tomato.consensus” : null}, {“tomato.consensus” : {$exists : true}}] })

Now here is what you wanted to do with your query.

  1. Being able to apply two filters on the same field without repeating the key.

This post by @natac13, answers your question where you can use the implicit and operation to get the result.


  1. Now your second query is that the solution provided by @natac13 works only if both the values are using some kind of operators(Element Query Operator or Logical Query Operators) and it would not work in your case.

So, here is how you can do it without repeating the same field name.

db.movieDetails.count({“tomato.consensus” : {$eq:null, $exists : true}})

If you still have any query, feel free to get back to us. Happy Learning :slight_smile:

Thanks,
Shubham Ranjan
Curriculum Support Engineer

3 Likes

As in the example you’ve stated, it does work but only if both are operations and not a string or int value such as if I want to query for documents that have null in a field and filter out only the ones where the field exists. None the less @Shubham_13709 answered it with the $eq operator. And thanks a ton @natac13 for chipping in both the times, learned something from that too.

1 Like