简体   繁体   English

如何在MongoDB中将数据添加到(大量)嵌套集合中

[英]How to add data to a (heavily) nested collection in MongoDB

I am a noob at Python and MongoDB and would really appreciate your help with my problem. 我是Python和MongoDB的新手,非常感谢您对我的问题的帮助。 My collection in MongoDB looks like this: 我在MongoDB中的集合如下所示:

{
 "Segments" : [
                {
                  Devices : [
                              "IP" : "",
                              "Interfaces" : 
                                             [
                                               {
                                                 "Name" :""
                                               }
                                             ],
                             ],
                             "DeviceName" : "",
                             "SegmentName" : ""
                 }
                ]
}

I have an object like so: 我有一个像这样的对象:

Node Details: {'node:98a': ['Sweden', 'Stockholm', '98a-3470'], 'node:98b': ['Denmark', 'Copenhagen', '98b-3471', '98b-3472']}

I need to update the 'Name' within 'Interfaces' part in the collection above, with values from the Node Details dictionary. 我需要使用“节点详细信息”字典中的值更新上述集合中“接口”部分中的“名称”。 I have tried using $set, $addToSet, $push etc., but nothing is helping. 我尝试使用$ set,$ addToSet,$ push等,但是没有任何帮助。 I have already added the Segment and DeviceName information. 我已经添加了Segment和DeviceName信息。

The output should be as follows: 输出应如下所示:

{
     "Segments" : [
                    {
                      Devices : [
                                  {
                                   "Interfaces" : 
                                                 [
                                                   {
                                                     "Name" :"98a-3470"
                                                   }
                                                 ],
                                   "DeviceName" : "node:98a",
                                  }
                                  {
                                   "Interfaces" : 
                                                 [
                                                   {
                                                     "Name" :"98b-3471"
                                                   },
                                                   {
                                                     "Name" :"98b-3472"
                                                   }
                                                 ],
                                   "DeviceName" : "node:98b",
                                  }
                                 ],                                 
                      "SegmentName" : "segmentA"
                     }
                    ]
    }

Any help would be greatly appreciated. 任何帮助将不胜感激。 I have tried a lot in the MongoDB shell and also on Google, but to no avail. 我在MongoDB shell和Google上都尝试了很多,但都无济于事。 Thank you all. 谢谢你们。

Regards, trupsster 问候,trupster

[[ EDITED ]] Okay, here is what I have got so far after continuing to poke around after posing the question: I used the following query in MongoDB shell: [[编辑]]好的,这是我提出问题后继续四处寻找的结果:我在MongoDB shell中使用了以下查询:

db.test.mycoll.update({'Segments.SegmentName':'segmentA','Segments.Devices.Name':'node:98a'}, {$set: {"Segments.$.Devices.0.Interfaces.Name" : "98b-3470"}})

Now this inserted in the correct place as per my 'schema', but when I try to add the second interface, it simply replaces the earlier one. 现在,按照我的“模式”将其插入正确的位置,但是当我尝试添加第二个接口时,它只是替换了先前的接口。 I tried using $push (complained about it not being an array), and $addToSet (showed another error), but none helped. 我尝试使用$ push(抱怨它不是数组)和$ addToSet(显示另一个错误),但是没有帮助。 Can you please help me from this point on? 从现在开始,您能帮我吗?

Thanks, trupsster 谢谢,特鲁斯特

[[ Edited again ]] [[再次编辑]]

I found the solution! 我找到了解决方案! Here is what I did: To add an interface to an existing device: 这是我所做的:将接口添加到现有设备:

db.test.mycoll.update({'Segments.SegmentName':'segmentA','Segments.Devices.Name':'node:98a'}, {$addToSet: {"Segments.$.Devices.0.Interfaces.Name" : "98a-3471"}})

Now, to append to the dict with a new 'Name' within the array 'Interfaces': 现在,在数组“接口”内添加一个新的“名称”以附加到字典上:

db.test.mycoll.update({'Segments.SegmentName':'segmentA','Segments.Devices.Name':'node:98a'}, {$addToSet: {"Segments.$.Devices.0.Interfaces" : {"Name" : "98a-3472"}}})

As you can see, I used $addToSet. 如您所见,我使用了$ addToSet。

Now, next step was to add the same information (with different values) to 2nd device, which was done like so: 现在,下一步是将相同的信息(具有不同的值)添加到第二台设备,方法如下:

db.test.mycoll.update({'Segments.SegmentName':'segmentA','Segments.Devices.Name':'node:98b'}, {$addToSet: {"Segments.$.Devices.1.Interfaces" : {"Name" : "98b-3473"}}})

So that was it! 就这样! I am so chuffed with myself! 我对自己很高兴! Thank you all who took time to read my problem. 谢谢所有花时间阅读我的问题的人。 I hope my solution will help someone. 希望我的解决方案能对某人有所帮助。

Regards, trupsster 问候,trupster

You did not say what you actually tried. 您没有说出您实际尝试过的内容。 To access a sub-document inside an array, you need to use dot notation with numeric indices. 要访问数组内的子文档,您需要将点符号与数字索引一起使用。 So to address the Name field in your example: 因此,在您的示例中解决“名称”字段:

Segments.Devices.0.Interfaces.0.Name

Did you try that? 你尝试过吗? Does it work? 它行得通吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM