在mongodb中的嵌套数组中插入数据 [英] Insert data in nested array in mongodb

查看:1391
本文介绍了在mongodb中的嵌套数组中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
MongoDB更新嵌套数组中的字段

Possible Duplicate:
MongoDB updating fields in nested array

我有类似的数据:

{ 
    "_id" : ObjectId("4f855061dd53351011000b42"), 
    "act_mgr" : [{ "sales" : {"agent" : ["rohan@walkover.in" ],  "last_interacted" : "rohan@walkover.in" } } ],
    "email" : "aman@asasas.com", "name" : "Aman",
    "sales" : [{"sno" : 1,  "message" : "description","status" : "open"},{"sno" : 12,"message" : "assad","status" :"open"}]
}

我想添加新代理并更新act_mgr中的last_interacted:销售类似的东西

I want to add new agent and update last_interacted in act_mgr:sales something like that

"act_mgr" : [{ "sales" : {"agent" : ["rohan@walkover.in","abc@walkover.in" ],
 "last_interacted" : "abc@walkover.in" } } ]

另外,如果我像开发人员一样添加新的act_mgr,那就就像

Also if I add new act_mgr like developer then it would be like

 "act_mgr" : [{ "sales" : {"agent" : ["rohan@walkover.in","abc@walkover.in" ],  "last_interacted" : "abc@walkover.in" } },
 { "developer" : {"agent" : ["newdeveloper@walkover.in" ],  "last_interacted" : "newdeveloper@walkover.in" } } ]

我不知道如何添加这些字段

I dont know how to add these fields

推荐答案

您可以使用以下更新语句来更新"act_mgr"数组内的嵌入式"sales"文档:

You can update the embedded "sales" document inside of the "act_mgr" array with the following update statement:

> db.sales.update({"act_mgr.sales.last_interacted":"rohan@walkover.in"}, {$push:{"act_mgr.$.sales.agent":"abc@walkover.in"}, $set:{"act_mgr.$.sales.last_interacted":"abc@walkover.in"}})
> db.sales.find().pretty()
{
    "_id" : ObjectId("4f855061dd53351011000b42"),
    "act_mgr" : [
        {
            "sales" : {
                "agent" : [
                    "rohan@walkover.in",
                    "abc@walkover.in"
                ],
                "last_interacted" : "abc@walkover.in"
            }
        }
    ],
    "email" : "aman@asasas.com",
    "name" : "Aman",
    "sales" : [
        {
            "sno" : 1,
            "message" : "description",
            "status" : "open"
        },
        {
            "sno" : 12,
            "message" : "assad",
            "status" : "open"
        }
    ]
}
> 

您可以将包含开发人员"信息的嵌入式文档添加到数组中,如下所示:

You can add the embedded document containing the "developer" information to the array like so:

> db.sales.update({"_id" : ObjectId("4f855061dd53351011000b42")}, {$push:{"act_mgr":{ "developer" : {"agent" : ["newdeveloper@walkover.in" ],  "last_interacted" : "newdeveloper@walkover.in" } }}})
> db.sales.find().pretty()
{
    "_id" : ObjectId("4f855061dd53351011000b42"),
    "act_mgr" : [
        {
            "sales" : {
                "agent" : [
                    "rohan@walkover.in",
                    "abc@walkover.in"
                ],
                "last_interacted" : "abc@walkover.in"
            }
        },
        {
            "developer" : {
                "agent" : [
                    "newdeveloper@walkover.in"
                ],
                "last_interacted" : "newdeveloper@walkover.in"
            }
        }
    ],
    "email" : "aman@asasas.com",
    "name" : "Aman",
    "sales" : [
        {
            "sno" : 1,
            "message" : "description",
            "status" : "open"
        },
        {
            "sno" : 12,
            "message" : "assad",
            "status" : "open"
        }
    ]
}
> 

可以在更新"文档中找到有关$ push和$ set修饰符的文档: http://www.mongodb.org/display/DOCS/Updating

The documentation on the $push and $set modifiers may be found in the "Updating" documentation: http://www.mongodb.org/display/DOCS/Updating

有关使用Mongo db创建和更新嵌入式文档的更多信息,请参见标题为点表示法(到达对象)"的文档. http://www.mongodb.org/display/DOCS/Dot+Notation+% 28Reaching + into + Objects%29

More information on creating and updating embedded documents with Mongo db may be found in the documentation titled "Dot Notation (Reaching into Objects)" http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29

有关使用"$"位置运算符更新嵌入文档的信息,可在更新"文档的"$位置运算符"部分中找到.
http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator

Information on updating embedded documents using the "$" positional operator may be found in the "The $ positional operator" section of the "Updating" documentation.
http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator

警告:通常,嵌入文档都匹配相同的结构是更常见的,因此可以更轻松地引用各个嵌入文档.您的销售"数组就是一个很好的例子.每个嵌入的文档包含相同的键"sno","message"和"status"

A word of caution: It is generally more common to have embedded documents all match the same structure, so that individual embedded documents may be referenced more easily. Your "sales" array is a good example of this; each embedded document contains the same keys, "sno", "message", and"status"

但是,"act_mgr"数组中的嵌入式文档包含不同的密钥.第一个包含销售",第二个包含开发人员".相反,请考虑以下结构:

However, the embedded documents inside your "act_mgr" array contain different keys; the first contains "sales", and the second contains "developer". Instead, maybe consider the following structure:

"act_mgr" : [
    {
        "title" : "sales",
        "agent" : [
            "rohan@walkover.in",
            "abc@walkover.in"
        ],
        "last_interacted" : "abc@walkover.in"
    },
    {
        "title": "developer",
        "agent" : [
            "newdeveloper@walkover.in"
        ],
        "last_interacted" : "newdeveloper@walkover.in"
    }
]

现在,每个嵌入式文档都包含相同的键,即标题",代理"和"last_interacted".

Now, each embedded documents contain the same keys, "title", "agent", and "last_interacted".

您可以使用以下命令更新子文档.

You could update sub-documents with the following command.

> db.sales.update({"act_mgr.title":"sales"}, {$push:{"act_mgr.$.agent":"abc@walkover.in"}, $set:{"act_mgr.$.last_interacted":"abc@walkover.in"}})

希望这将使您能够进行所需的更新,并可能为您提供有关架构设计的一些思考.祝你好运!

Hopefully this will allow you to make the updates that you need to, and perhaps give you some food for thought regarding schema design. Good luck!

这篇关于在mongodb中的嵌套数组中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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