3

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.

7

"$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.

  • I understand now. That's why they kept referring to queries as documents because they are in fact JSON documents, I still think in terms of relational DB approach. – Marina Dunst Feb 29 '16 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.