bouncingHippo
bouncingHippo

Reputation: 6040

are the following mongodb queries equivalent?

so i have the below query that does NOT return anything when it should.

db.food.find({ingredient : {name : {$ne : "Kahlua"}}});  //empty data

However, what i think is its equivalent does output the correct info:

db.food.find({"ingredient.name" : {$ne : "Kahlua"}});   //gives correct data

I tried this using the BrowserShell for the above 2 queries, and it hasn't given me much problem for simple functions and queries.

Upvotes: 1

Views: 60

Answers (1)

AdaTheDev
AdaTheDev

Reputation: 147224

No, they are not the same.

db.food.find({"ingredient.name" : {$ne : "Kahlua"}}); 

is saying "find where the ingredient name is not equal to Kahlua", whereas...

db.food.find({ingredient : {name : {$ne : "Kahlua"}}});

is saying "find where the ingredient sub object consists of a name property only, where that name is not equal to Kahlua". So in this case you're actually performing a search on the whole subject.

Probably far better explained here - dot notation vs subobjects

Upvotes: 2

Related Questions