使用Python插入MongoDB集合中的嵌套字段
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论