I'm reading over the MongoDB manual. Some examples, have quotes around the key values, e.g: db.test.find({"_id" : 5})
and others don't, e.g: db.test.find({_id : 5})
Both quoted and un-quoted versions work. But I'm wondering if there are some nuanced difference here I don't know about or is one a preferred best practice?
Thanks.
In JavaScript (the language of the MongoDB shell) those are treated exactly the same. The quotes are needed, however, when a key contains a period like when you're using dot notation to match against an embedded field as in:
db.test.find({"name.last": "Jones"})
My preference is to not use the quotes unless they're needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With