本文介绍了密钥名称中的MongoDB点(。)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎mongo不允许插入带点(。)或美元符号($)的键,但是当我使用mongoimport工具导入包含点的JSON文件时,它工作正常。驱动程序抱怨尝试插入该元素。

It seems mongo does not allow insertion of keys with a dot (.) or dollar sign ($) however when I imported a JSON file that contained a dot in it using the mongoimport tool it worked fine. The driver is complaining about trying to insert that element.

这是文档在数据库中的样子:

This is what the document looks like in the database:

{
    "_id": {
        "$oid": "..."
    },
    "make": "saab",
    "models": {
        "9.7x": [
            2007,
            2008,
            2009,
            2010
        ]
    }
}

我这样做是错误的,不应该使用像外部数据那样的哈希映射(即模型)还是可以以某种方式逃避点?也许我在想类似Javascript。

Am I doing this all wrong and should not be using hash maps like that with external data (i.e. the models) or can I escape the dot somehow? Maybe I am thinking too much Javascript-like.

推荐答案

MongoDB不支持的键,因此您必须预先处理JSON文件以在导入之前删除/替换它们或者你会为各种各样的问题做好准备。

MongoDB doesn't support keys with a dot in them so you're going to have to preprocess your JSON file to remove/replace them before importing it or you'll be setting yourself up for all sorts of problems.

这个问题没有一个标准的解决方法,最好的方法太依赖于细节情况。但是如果可能的话,我会避免使用任何关键的编码器/解码器方法,因为你会继续为此永久性地付出不便,因为JSON重组可能是一次性成本。

There isn't a standard workaround to this issue, the best approach is too dependent upon the specifics of the situation. But I'd avoid any key encoder/decoder approach if possible as you'll continue to pay the inconvenience of that in perpetuity, where a JSON restructure would presumably be a one-time cost.

这篇关于密钥名称中的MongoDB点(。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 10:33