patternMinor
Are operators also considered to be keys?
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
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.
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.
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.