0

I am using a dynamic key that is a series of numbers. Because you have to double quote numbers that are keys in JSON, is there any difference between using a dynamic key that is a number turned to a string versus an underscored number that's a string?

Basically, the difference of converting a number to a string versus concatenating it with an underscore (which turns it to a string).

I DOESN'T seem like there is a difference, however, I'd like to ask this question to everyone because sometimes an unexpected difference does turns up.

{ "1" : "some content",
  "2" : "some more content"
}

versus

{ _1 : "some content",
  _2 : "some more content"
}

Thanks.

cathy.sasaki
  • 3,995
  • 6
  • 28
  • 39
  • 1
    Is this a particular magical feature of MongoDB? Because `{ _1: ".." }` is *not* valid [JSON](http://json.org): all JSON keys must be strings. It is a valid JS Object Literal, but _1 (which is treated as "_1" when used as a property name) is different from "1". – user2246674 May 25 '13 at 20:24
  • Hmm. MongoDb accepts non-string keys (object literals) if they are not numbers. I guess the next question is if Mongodb sees a difference between non-string and string keys? – cathy.sasaki May 25 '13 at 20:29
  • No idea - I don't use MongoDB. However, consider emphasizing MongoDB's behavior in the question and title to help focus answers. A better title might be something similar to: "What difference/behavior do unquoted keys have in MonogoDB?" (Well, this isn't that great either, but hopefully it should help title focus and move the question to Mongo-specific vs. JSON-specific.) – user2246674 May 25 '13 at 20:33
  • Good suggestion. I updated title. – cathy.sasaki May 25 '13 at 20:35
  • only strings can be used as keys. What mongo *shell* does is figure out if the key isn't quoted it still has to be a string. – Asya Kamsky May 26 '13 at 04:37

1 Answers1

2

Note that it is not JSON that you are sending to MongoDb but javascript objects. The difference here). And for this reason the following expressions are equivalent.

// unquoted key
db.col.insert({ 1: "key 1 unquoted"})

// quoted key
db.col.insert({ "1": "key 1 quoted"})

So back to your question, the only difference is that in one case you have 1 as key, and in the other _1.

But of course, it depends also what drivers you use to write this data to Mongo. They might be guilty if you see any difference between quoting or not-quoting the keys. Testing in the mongo shell, you get the same results.

The above is all true for top level keys. But if you have keys 1, 2, 3 on other levels, things can become tricky, and for this reason I recommend you not to use numbers as keys. The problems come from the Mongo query syntax when handling arrays.

Assume the following document into a collection:

{
    "foo": {
        "0": "abc"
    },
    "bar": [ "x", "y", "z"]
}

Both queries below are valid:

db.col.find({ "foo.0": "abc" })
db.col.find({ "bar.0": "x" })

Just the semantics is different:

  • in the former query you query for the documents containing a foo key which is an object having a key 0 with value abc
  • in the latter query you ask for the documents containing a bar key which is an array having x on the first position (0)

is interpreted

Community
  • 1
  • 1
Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168
  • Okay. Thanks. When you use mongoimport, it rejects numbered keys that are not strings, but when you do an insert (as you put above) it doesn't care. That makes sense, because you are important a JSON file vs being inside the mongodb environment. – cathy.sasaki May 25 '13 at 20:51