使用Python插入MongoDB集合中的嵌套字段

发布于 2025-01-28 23:46:46 字数 1950 浏览 2 评论 0原文

我是MongoDB的新手,在使用Python插入MongoldB收集的嵌套值中寻求一些帮助。

我在MongoDB中有一个collection(test),它已经有许多字段。现在

[0:

{
A:1,

B:5,

C:10,

D:

{
   F1: 67

   F2: 54
 }

},

1: {

A:3

B:8

C:100,

D:{

   F1: 60,

   F2: 4,
 }}
,

2:{

A:15,

B:51,

C:100,

D:{

   F1: 97

   F2: 32
 }

}]

,我在一个python脚本中创建了一些新字段,该脚本为每个索引生成一些值,这是我为每个索引所拥有的结果JSON:

{0:

   {Newfield:
       
        {nested field:{ 

                            Value1: 6,

                            Value 2: 8,

                            Value 3: 11,

                           }

      }
1: {Newfield:
       
        {nested field:{ 

                            Value1: 64,

                            Value 2: 83,

                            Value 3: 21,

                           }

      }
2: {Newfield:
       
        {nested field:{ 

                            Value1: 89,

                            Value 2: 65,

                            Value 3: 34,

                           }

      }
}

现在我想将这些字段添加到此现有集合中,因此新集合将看起来像这样:

[0: {
     A:1,

B:5,

C:10,

D:{

   F1: 67

   F2: 54
 }

Newfield:

       {nested field:{ 

                            Value1: 6,

                            Value 2: 8,

                            Value 3: 11,

                           }


      }
}
,

1: {

A:3

B:8

C:100,

D:{

   F1: 60,

   F2: 4,

   }

Newfield:

       {nested field:{ 

                            Value1: 64,

                            Value 2: 83,

                            Value 3: 21,

                           }

      }
,

2:{

A:15,

B:51,

C:100,

D:{

   F1: 97

   F2: 32

 }

Newfield:

       {nested field:{ 

                            Value1: 89,

                            Value 2: 65,

                            Value 3: 34,

                           }

      }

}]

原始集合中有多个行,因此我正在寻找一个循环或创建嵌套字段的东西,然后用每行的JSON结果文件填充它。 任何建议将不胜感激

I am new to mongoDb and looking for some help in inserting nested values to mongoldb collection using python.

I Have a collection in mongoDB called(test) that already has a number of fields in it..and looks something like this

[0:

{
A:1,

B:5,

C:10,

D:

{
   F1: 67

   F2: 54
 }

},

1: {

A:3

B:8

C:100,

D:{

   F1: 60,

   F2: 4,
 }}
,

2:{

A:15,

B:51,

C:100,

D:{

   F1: 97

   F2: 32
 }

}]

Now I created some new fields in a python scripts that generates some value for each index,This is the result json that I have for each index:

{0:

   {Newfield:
       
        {nested field:{ 

                            Value1: 6,

                            Value 2: 8,

                            Value 3: 11,

                           }

      }
1: {Newfield:
       
        {nested field:{ 

                            Value1: 64,

                            Value 2: 83,

                            Value 3: 21,

                           }

      }
2: {Newfield:
       
        {nested field:{ 

                            Value1: 89,

                            Value 2: 65,

                            Value 3: 34,

                           }

      }
}

now I want to add these fields to this existing collection, so the new collection would look something like this:

[0: {
     A:1,

B:5,

C:10,

D:{

   F1: 67

   F2: 54
 }

Newfield:

       {nested field:{ 

                            Value1: 6,

                            Value 2: 8,

                            Value 3: 11,

                           }


      }
}
,

1: {

A:3

B:8

C:100,

D:{

   F1: 60,

   F2: 4,

   }

Newfield:

       {nested field:{ 

                            Value1: 64,

                            Value 2: 83,

                            Value 3: 21,

                           }

      }
,

2:{

A:15,

B:51,

C:100,

D:{

   F1: 97

   F2: 32

 }

Newfield:

       {nested field:{ 

                            Value1: 89,

                            Value 2: 65,

                            Value 3: 34,

                           }

      }

}]

There a multiple rows in original collection and result so I am looking for a for loop or something that creates the nested field and then populates it with json result file for each row.
Any advice would be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文