Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I deal with dots in MongoDB key names?

I'm developing this piece of software in Node and MongoDB in which I essentially want to store versions of packages with the following structure:

{
    "versions":
    {
        "1.2.3": { stuff }
    }
}

(similar to how npm does things in couch)

The issue is that when I updated MongoDB I discovered that it doesn't allow dots in key names (due to dot notation existing), causing my code to fail. After researching this, all I could find is that you need to transform the dots to some other character before storing in the db, then transform them back again when accessing. Is there really no better way to deal with this?

If there isn't, how can I do this transformation without copying the data over to another key and deleting the original?

like image 262
jli Avatar asked Jul 19 '12 01:07

jli


People also ask

What does three dots mean in MongoDB?

Three '.'s means your statement isn't complete... Most likely, you've missed a closing curly brace.

What is the use of the dot notation in MongoDB?

MongoDB uses the dot notation to access the elements of an array and to access the fields of an embedded document.

How do I search for keywords in MongoDB?

In MongoDB, we can perform text search using text index and $text operator. Text index: MongoDB proved text indexes that are used to find the specified text from the string content. Text indexes should be either a string or an array of string elements.


1 Answers

Can you use a collection of versions with stuff?

Like:

{
    "versions":
    [
              {
                   "version_num": "1.2.3",
                   "stuff": { stuff }
              },
              {
                   "version_num": "1.2.4",
                   "stuff": { stuff }
              }
    ]
}
like image 153
keaplogik Avatar answered Nov 01 '22 19:11

keaplogik