首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向Mongo文档添加新元素

向Mongo文档添加新元素
EN

Stack Overflow用户
提问于 2013-12-10 19:31:51
回答 1查看 33关注 0票数 1

我想插入标题是'Cities‘的数组元素,但它不插入或插入到新数组而不是Links.Link中。知道我做错了什么吗。我已经尝试过push和$addToSet

代码语言:javascript
复制
{
    "Component" : [
        {
            "Title" : "regions",
            "Links" : {
                "Link" : [

                    {
                        "Text" : "Tyrol",
                        "Url" : "/1xf-en700p/cheap-holidays-tyrol",
                        "Title" : "Cheap holidays Tyrol"
                    },
                    {
                        "Text" : "Browse Regions ",
                        "Url" : "/1xf-en6ujz-10ts/cheap-holidays-austria/regions",
                        "Title" : "Cheap holidays Austria",
                        "Style" : "BrowseForMore"
                    }
                ]
            }
        },
        {

            "Title" : "cities",
            "Links" : {
                "Link" : [
                    {
                        "Text" : "Maria Saal",
                        "Url" : "/1xf-enu4dl/cheap-holidays-maria-saal",
                        "Title" : "Cheap holidays Maria Saal"
                    },
                    {
                        "Text" : "Nessendorf",
                        "Url" : "/1xf-enwvu8/cheap-holidays-nessendorf",
                        "Title" : "Cheap holidays Nessendorf"
                    },

                ]
            }
        },

    ],
    "Id" : "125570",

}

with 

var pd = { Text: 'test',Url: 'test',Title: 'test' };
db.PopularDestinationsUk.update
( 
    {
         Id: '125570'
        ,'Component.Title': 'cities'}

    }
    ,
    { 
        $push: 
        {
            'Component.Link.$.Links { Text: 'NEW',Url: 'NEW',Title: 'NEW' }}}
        }
    }
)

但是它没有插入到正确的位置

EN

回答 1

Stack Overflow用户

发布于 2013-12-10 19:38:26

按如下方式更新您的查询:

代码语言:javascript
复制
db.PopularDestinationsUk.update({Id: "125570", "Component.Title" : "cities"}, {$push : {"Component.$.Links.Link" : {Text: 'NEW',Url: 'NEW',Title: 'NEW'}}})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20493347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档