Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In mongodb, need quotes around keys for CRUD operations, example: "_id" vs _id?

Tags:

mongodb

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.

like image 413
cathy.sasaki Avatar asked May 11 '13 15:05

cathy.sasaki


1 Answers

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.

like image 147
JohnnyHK Avatar answered Nov 16 '22 04:11

JohnnyHK