HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Are operators also considered to be keys?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
areconsideredalsokeysoperators

Problem

I'm taking a MongoDB course and one of the assignments asks to include quotes around keys.

So I did for a query like this {$push: {"key":value}}

But I was told that $push is also a key because it's on the left hand side. I always thought keys were like identifiers for values whereas operators are just actions, they don't have any value associated with.

Solution

"$push" is a key in the JSON context. Don't forget everything in mongo (queries, projections, documents, method parameters) are represented as JSON objects. So, although $push is an operator for Mongo, it is still a key on the JSON object it belongs to.

JSON's specification demands key names to be surrounded by double quotes, although the Mongo shell, which is actually a Javascript interpreter, is more flexible and allows key names (field names and operators) without them, as the language does.

Context

StackExchange Database Administrators Q#130715, answer score: 7

Revisions (0)

No revisions yet.