Why we use $ sign in expr

What will happen if we wont use $ sign in the given expression

{"$expr":{"$eq": ["$start station id","$end station id"]}}

Why 0 results for the below query
{"$expr":{"$eq": [“start station id”, “end station id”]}}

Hey @YASH_VARMA, Welcome to MongoDB university forum,

See $expr,

Allows the use of aggregation expressions within the query language.

$expr can build query expressions that compare fields from the same document in a match condition.

See Expressions:

Expressions can include field paths, literals, system variables, expression objects, and expression operators. Expressions can be nested.

Field Paths

Aggregation expressions use field path to access fields in the input documents. To specify a field path, prefix the field name or the dotted field name (if the field is in the embedded document) with a dollar sign $ . For example, "$user" to specify the field path for the user field or "$user.name" to specify the field path to "user.name" field.

"$<field>" is equivalent to "$CURRENT.<field>" where the CURRENT is a system variable that defaults to the root of the current object, unless stated otherwise in specific stages.


Above details explains everything,

In-sort without $ reference this “start station id” will consider as string value.

2 Likes

Hi @YASH_VARMA, as @turivishal rightly mentioned without the “" sign it will be treated as a regular string, instead we use "” sign to refer to the specific property of the document. In case you have any doubts please refer to the docs attached by Vishal and feel free to reach out to us.
Thanks. Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

2 Likes

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