相关疑难解决方法(0)

Mongodb更新了深层嵌套的子文档

我有一个深层嵌套的文档结构,如下所示:

{id: 1, 
 forecasts: [ { 
             forecast_id: 123, 
             name: "Forecast 1", 
             levels: [ 
                { level: "proven", 
                  configs: [
                            { 
                              config: "Custom 1",
                              variables: [{ x: 1, y:2, z:3}]
                            }, 
                            { 
                              config: "Custom 2",
                              variables: [{ x: 10, y:20, z:30}]
                            }, 
                    ]
                }, 
                { level: "likely", 
                  configs: [
                            { 
                              config: "Custom 1",
                              variables: [{ x: 1, y:2, z:3}]
                            }, 
                            { 
                              config: "Custom 2",
                              variables: [{ x: 10, y:20, z:30}]
                            }, 
                    ]
                }
            ]
        }, 
    ]

}
Run Code Online (Sandbox Code Playgroud)

我正在尝试更新集合以插入新的配置,如下所示:

newdata =  {
  config: …
Run Code Online (Sandbox Code Playgroud)

mongodb

58
推荐指数
5
解决办法
6万
查看次数

在MongoDB中检索n级深层子文档

我在mongoDB中有一个深度嵌套的文档,我想获取单个子对象.

例:

{
   "schoolName": "Cool School",
   "principal": "Joe Banks",
   "rooms": [
      {
         "number": 100
         "teacher": "Alvin Melvin"
         "students": [
            {
               "name": "Bort"
               "currentGrade": "A"
            },
            // ... many more students
         ]
      },
      // ... many more rooms
   ]
}
Run Code Online (Sandbox Code Playgroud)

最近Mongo更新为允许使用$ elemMatch投影进行1级深度子对象检索:

var projection = { _id: 0, rooms: { $elemMatch: { number: 100 } } };
db.schools.find({"schoolName": "Cool School"}, projection);
// returns { "rooms": [ /* array containing only the matching room */ ]  }
Run Code Online (Sandbox Code Playgroud)

但当我尝试以同样的方式取一个学生(2级深)时,我收到一个错误:

var projection …
Run Code Online (Sandbox Code Playgroud)

javascript mongodb

12
推荐指数
1
解决办法
7844
查看次数

标签 统计

mongodb ×2

javascript ×1