Difficulty in understanding difference in two queries

Dear All,

I’m new to MongoDB and started learning it recently. While working on Lab exercise to solve that I wrote first query which is not working whereas second one is from answer help and is working. I just wanted to know what is wrong with my query. Although I know mine is not a good way of writing it but I think it should have worked also may not be performant.

Please help.

db.listingsAndReviews.find({"$and":[{“accommodates”:{"$gt":6}},{“reviews”:{"$size":50}]}).count()

db.listingsAndReviews.find({ “reviews”: { “$size”:50 },“accommodates”: { “$gt”:6 }}).count()

Regards
Manoj

Hi @Manoj_Kumar6,

Welcome to the MongoDB Community Forum,

We hope that you are enjoying the MongoDB University Courses. :sparkles:

Here your query is perfect, just you have missed one curly braces and I think that is creating error for you.

So the correct version will be as :small_red_triangle_down:

db.listingsAndReviews.find({"$and": [{"accommodates": {"$gt": 6 }},
                           {"reviews":{"$size": 50 } } ] } ).count()

We suggest you use the proper formatting tool while writing MongoDB queries so you don’t get lost in too many curly braces.

We wish you happy learning :100:
In case of any questions, feel free to reach out.

All the best,
Kushagra

1 Like

Hi @Kushagra_Kesav,

Thanks a lot for your reply. I got that. Unfortunately I was not getting any error in that it looked like infinite execution to me.

Yes, I’m enjoying MongoDB University Courses.

Thanks for your suggestion for formatting tool. I’ll take care of it next time.

Regards
Manoj

1 Like