Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quote or Non quote deference in MongoDB query

As an example,either of these will work:

db.wands.insert({"name": "Dream Bender", "creator": "Foxmond"})
db.wands.insert({name: "Dream Bender", creator: "Foxmond"})

Which one will I use any why?

like image 774
saif Avatar asked Aug 08 '17 18:08

saif


People also ask

How does $in work in MongoDB?

Use the $in Operator to Match Values This query selects all documents in the inventory collection where the value of the quantity field is either 5 or 15. Although you can write this query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.

What is the select query in MongoDB?

Running SQL statements against a MongoDB collection. An SQL SELECT statement typically retrieves data from tables in a database, much like a mongo shell find statement retrieves documents from a collection in a MongoDB database.

Can we use where clause in MongoDB?

The MongoDB $where operator is used to match documents that satisfy a JavaScript expression. A string containing a JavaScript expression or a JavaScript function can be pass using the $where operator.

Where is less than in MongoDB?

MongoDB provides different types of comparison operators and less than operator ( $lt ) is one of them. This operator is used to select those documents where the value of the field is less than (<) the given value. You can use this operator in methods like, find() , update() , etc.


1 Answers

You can use quotes and never have to worry about when they are necessary.

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 without an underscore or alpha character would have to be quoted.

So, either of these will work:

db.wands.insert({"name": "Dream Bender", "creator": "Foxmond"})
db.wands.insert({name: "Dream Bender", creator: "Foxmond"})

Also, this would work:

db.wands.insert({"3name": "Dream Bender", "creator": "Foxmond"})

But this won't work.

db.wands.insert({3name: "Dream Bender", creator: "Foxmond"})

So, I find it easier to be in the habit of using quotes so you don't have to remember the exceptions.

like image 85
H.P. Avatar answered Oct 20 '22 00:10

H.P.