2014년 12월 13일 토요일

significance of double quotes for field name

Does anybody know why double quotes are used for field name. 
The query seems to work with or without. 


For example - "_id"  below 

db.zips.aggregate({$group:{_id:"$state",totalPop:{$sum:"$pop"}}})

db.zips.aggregate({$group:{"_id":"$state",totalPop:{$sum:"$pop"}}})






Does anybody know why double quotes are used for field name. 
The query seems to work with or without. 

Hi,

When you run a query or command from the `mongo` shell interface it is being parsed by the shell's JavaScript interpreter.

Anything which isn't valid JavaScript will cause a syntax error. For example, a collection or field name starting with a digit rather than an underscore or alpha character would have to be quoted:

> db.movies.find({3dmovie: 'Jaws III'})
2014-10-31T09:46:58.457+1000 SyntaxError: Unexpected token ILLEGAL

Names like _id, $sum, and totalPop are valid JavaScript so do not have be strictly quoted in the `mongo` shell.

In general I find it easier to be in the habit of using quotes so you don't have to remember the exceptions, although you can save on a few keystrokes without ;-)



Thank you for taking time to answer.


댓글 없음:

댓글 쓰기