Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dot in field name?

Tags:

mongodb

How to use dot in field name ?

I see error in example:

db.test2.insert({ "a.a" : "b" })  can't have . in field names [a.a] 
like image 222
Bdfy Avatar asked Dec 08 '11 10:12

Bdfy


People also ask

Can JSON field names have dots?

Unlike MongoDB, which does not allow dots, ( . ), in JSON or BSON field names, IBM® Informix® conforms to the JSON standard and allows dots. For example: {"user. fn" : "Jake"}. However, you cannot run a query or an operation directly on a field that has a dot in its name.

Can JSON keys have dots?

It's impossible to use a literal dot in a JSON key with FileMaker's JSON parsing functions.

Why dollar is used in MongoDB?

According to the docs, a "$" is reserved for operators. If you look at the group operator however, values need to have a dollar prefixed. These values are not operators.

How do I get field names in MongoDB?

You can use $getField to retrieve the value of fields with names that contain periods ( . ) or start with dollar signs ( $ ).


2 Answers

You can replace dot symbols of your field name to Unicode equivalent of \uff0E

db.test.insert({"field\uff0ename": "test"}) db.test.find({"field\uff0ename": "test"}).forEach(printjson) { "_id" : ObjectId("5193c053e1cc0fd8a5ea413d"), "field.name" : "test" } 

See more:

  1. http://docs.mongodb.org/manual/faq/developers/#faq-dollar-sign-escaping
  2. http://docs.mongodb.org/manual/core/document/#dot-notation
like image 56
Fisk Avatar answered Sep 17 '22 16:09

Fisk


Actualy you may use dots in queries. See: http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29

Because of this special dot symbol mean you cannot use it in field names. Like you cannot use dot symbol in identifiers in most of programming languages.

You may write query db.test2.find({ "a.a" : "b" }) but if you want to be able to write such a query you need to insert your object like so: db.test2.insert({"a": {"a": "b"}}). This will create document with the field named "a" with the value of embeded document containing the field named "a" (again) with the value "b".

like image 22
lig Avatar answered Sep 17 '22 16:09

lig