0


Edit: This is not a javascript question. This is my observation on the Mongodb console.

I am a newbee to Mongodb, a bit confused about the syntax for query commands. Some commands require quotes and some do not. Here is an example:

Insert one item:

db.users.insert(
{
 "name": "Ben Bautista",
 "contact": "987648711",
 "address": [
   {
     "building": "22 A, Bluemoon Apt",
     "zipcode": 90211
  },
  {
     "building": "170 A, Crompton Apt",
     "zipcode": 50910
  }

] } )

Query: The query for name works with or without quotes. But the query for nested item address.building throws a syntax error when no quotes used. (The third query below throws error).
Is there any guideline for querying nested items?

db.users.find({name:/Ben/}) 
db.users.find({"name":/Ben/}) 
db.users.find({address.building:/Bluemoon/})
db.users.find({"address.building":/Bluemoon/})
user6296218
  • 325
  • 1
  • 3
  • 7
  • Welcome to StackOverflow :). As per the linked [duplicate question](http://stackoverflow.com/questions/4348478/what-is-the-difference-between-object-keys-with-quotes-and-without-quotes), whether quotes are optional or required depends on whether a key is a valid JavaScript identifier. This is part of JavaScript syntax and not specific to MongoDB: strings including dots or dashes (or starting with numbers) have to be quoted. If in doubt, it's probably easier to be consistent and always use quotes. – Stennie May 06 '16 at 21:39
  • Update: I found an explanation in the Mongodb documentation.: When you use the dotted notation, you must enclose the dotted names in quotes. – user6296218 May 07 '16 at 14:25
  • The [`mongo` shell](https://docs.mongodb.com/manual/mongo/) is an interactive JavaScript interface to MongoDB, so behaviour such as whether quoting is required is inherited from the JavaScript interpreter. – Stennie May 07 '16 at 21:24

0 Answers0