admin管理员组

文章数量:1297114

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?

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?

Share asked Jul 19, 2012 at 1:22 jlijli 6,6232 gold badges30 silver badges37 bronze badges 16
  • Are you saying you already have data like that in Mongo? That should not have been possible even before the update. What version were you using? – Thilo Commented Jul 19, 2012 at 1:25
  • @Thilo I don't actually remember, but it may have been the driver that was buggy and allowed it. – jli Commented Jul 19, 2012 at 1:26
  • @c0deNinja It allows me to look up by version name without iterating through the whole array of potentially very many versions. – jli Commented Jul 19, 2012 at 1:26
  • So you do have data like that in the DB now? – Thilo Commented Jul 19, 2012 at 1:26
  • 1 @Thilo yep, fortunately this hasn't gone into production yet – jli Commented Jul 19, 2012 at 1:27
 |  Show 11 more ments

2 Answers 2

Reset to default 4

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 }
              }
    ]
}

Dot restrictions are currently driver enforced, and not all drivers have prevented dots in field names since the beginning. You can write raw protocol code to do all sorts of crazy stuff in Mongo, including using really weird characters in collection names.

You will be much better off if you clean that up (probably replace dots with - or some other valid character), but it's going to be difficult to do it well with any kind of intelligent filtering. You will most likely need to iterate through the entire collection, munge the values in your app, and then overwrite the entire "versions" field in your doc. In place overwrites this should be reasonably speedy since they won't resize the doc, and likely won't change any indexes.

本文标签: javascriptHow do I deal with dots in MongoDB key namesStack Overflow