Verifying if both queries are similar or not

I just want to verify that if both queries listed below are same or not.

db.listingsAndReviews.find({'property_type': 'House'}, {'amenities': {"$all": ['Changing table']}}).count()

db.listingsAndReviews.find({$and: [{'property_type': 'House'}, {'amenities': {"$all": ['Changing table']}}]}).count()

I think that both are same but if both are same why are they returning different values?
1st query gives 606 while the other one (with $and) gives 11. If someone could state a reason for this behaviour.

Hi @Yashvander_Bamel, welcome to the community.
No they both are different queries, please note that the first parameter to the find command is the query filter and the second parameter represents the projection(fields to be selected) that you want on the returned results.

So the first query would return all the documents that have a property_type as House but the second query would only return the documents that have property_type as House and the amenities array is equal to ['Changing table'].

I hope it helps.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like

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