$eq Query Operato

40 views
Skip to first unread message

Manuele Cappelleri

unread,
Sep 9, 2016, 12:21:42 PM9/9/16
to mongodb-user
Hello all,
can someone explain why this query does not throw an error?

{ $match: { 'key': {'$eq': { '$eq': 'value' } } }}

IMHO, this should lead to an error as it cannot be evaluated.

If I am not wrong, could we summarize a general rule stating that:

predicate to $eq cannot be an operator itself or an object containing keys that start with $

Thank you all in advance!


Nishant Bhardwaj

unread,
Sep 14, 2016, 1:15:11 AM9/14/16
to mongodb-user

Hello Manuelle,

Few points to note here:

  • By design, the $eq comparison query operator matches a field against a Value, Document value or an Array value.
  • Field names must not start with a dollar sign (i.e. $), I recommend reviewing Naming Restrictions in the MongoDB manual.
  • The inner most '$eq' will be treated as a field of a sub-document, which cannot exist as per the naming convention stated above.

Thus the query does not match any documents and returns no result.

Regards,

Nishant

Manuele Cappelleri

unread,
Sep 15, 2016, 5:29:28 AM9/15/16
to mongodb-user
Thank you Nishant,
the naming restriction was the main reason I wrote the post.

My point is: given that naming restriction disallow field names starting with $,
the above query SHOULD fail and throw an error as it is impossible to evaluate.
Allowing this query to run, imho, implicitly says that there MIGHT be a result, which is obviously wrong as per your reply....

Don't you agree?
Reply all
Reply to author
Forward
0 new messages