键名中的 MongoDB 点 (.) [英] MongoDB dot (.) in key name

查看:41
本文介绍了键名中的 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 点 (.)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆