Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of $eq

Tags:

mongodb

I noticed a new $eq operator released with MongoDB 3.0 and I don't understand the purpose of it. For instance these two queries are exactly the same:

db.users.find({age:21})

and

db.users.find({age:{$eq:21}})

Does anyone know why this was necessary?

like image 395
jwerre Avatar asked Mar 12 '15 16:03

jwerre


1 Answers

The problem was that you'd have to handle equality differently from comparison when you had some kind of query builder, so it's

{ a : { $gt : 3 } }
{ a : { $lt : 3 } }

but

{ a : 3 }

for equality, which looks completely different. The same applies for composition of $not, as JohnnyHK already pointed out. Also, comparing with $eq saves you from having to $-escape user provided strings. Therefore, people asked for alternatives that are syntactically closer and it was implemented. The Jira ticket contains a longer discussion which mentions all these points.

The clearer syntax of an $eq operator might also make sense in the aggregation framework to compare two fields, should such a feature be implemented.

Also, the feature has apparently been around since 2.5, was added to the documentation relatively late.

like image 86
mnemosyn Avatar answered Sep 21 '22 08:09

mnemosyn