在MongoDB中保存虚线字段 [英] Save dotted field in MongoDB

查看:82
本文介绍了在MongoDB中保存虚线字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将结果从JSON API保存到MongoDB集合。
我试图保存的JSON数据具有以下结构:

I'am trying to save the outcome from an JSON API to a MongoDB collection. The JSON data I'am trying to save has the following structure:

compatibility: {
    2.7.1: {
        2.2.6: [
            100,
            1,
            1
        ]
     },
     2.8.3: {
         2.2.6: [
             100,
             2,
             2
         ]
     }
 }

但是我收到以下错误:'compatibility.2.7.1'中的虚线字段'2.7.1'对存储无效。

But I'am getting the following error: The dotted field '2.7.1' in 'compatibility.2.7.1' is not valid for storage.

有谁知道如何解决这个问题?

Does anyone know how to fix this?

推荐答案

你在字段名称中不能有。点是mongodb有些特别。例如,如果你插入

You can't have . in field names. Dot is some how special in mongodb. For example if you insert

db.test.insert({
   a: {
      b: 2
   }
});

然后你可以搜索该项目

db.test.find({
   'a.b': 2
});

因此想象一下你可以在字段名称中加点并插入

So imagine you are able to have dot in field name and you insert

db.test.insert({
   a: {
      b: 2
   },
   'a.b': 3
});'

然后用<$搜索会很奇怪c $ c>'a.b',不是吗?

这篇关于在MongoDB中保存虚线字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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