在MongoDB中插入多个嵌套元素

时间:2016-06-30 21:01:37

标签: mongodb nested multiple-instances

我希望在多个嵌套文档中插入另一个文档。您对如何解决它有任何其他想法吗?感谢

{
"_id" : ObjectId("576bddaa08b8f7dc20000033"),
"Name" : "Business",
"Image" : "fa fa-dollar",
"Description" : "Money, Capitalism, Business",
"NoForums" : 0,
"NoTopics" : 0,
"NoComments" : 0,
"NoUsers" : 0,
"CoverImage" : "https://www.rocklandtrust.com/assets/content/txh9vvwg/2014/08/06/business%20savings.jpg",
"Children" : [ 
    {
        "_id" : ObjectId("57715e0608b8f7441b00002d"),
        "Name" : "Business School",
        "URLName" : "Business-School",
        "Image" : "http://www.small-business-website.net/wp-content/uploads/2016/04/How-to-achieve-a-successful-business-sale-128x128.jpg",
        "Description" : "Everything about Business Schools",
        "NoForums" : 0,
        "NoTopics" : 0,
        "NoComments" : 0,
        "NoUsers" : 0,
        "CoverImage" : "http://www.incae.edu/sites/default/files/styles/slideshow-un-medio/public/primerdiamba2010_018.jpg?itok=LfZmxw14",
        "Children" : []
    }, 
    {
        "_id" : ObjectId("57715f0d08b8f7942c00002b"),
        "Name" : "Journals",
        "URLName" : "Journals",
        "Image" : "http://www.alliedacademies.org/images/accounting.png",
        "Description" : "Journals and Diaries about Businesses",
        "NoForums" : 0,
        "NoTopics" : 0,
        "NoComments" : 0,
        "NoUsers" : 0,
        "CoverImage" : "http://www.advantagebusinessvaluations.com/wp-content/uploads/2015/08/slide3.jpg",
        "Children" : []
    }, 
    {
        "_id" : ObjectId("57740ae208b8f7f41a000039"),
        "Name" : "Stock Exchange",
        "URLName" : "Stock-Exchange",
        "Image" : "https://38.media.tumblr.com/avatar_bd29383b5657_128.png",
        "Description" : "Stock Exchange, US Stock",
        "NoForums" : 0,
        "NoTopics" : 0,
        "NoComments" : 0,
        "NoUsers" : 0,
        "CoverImage" : "http://c.fastcompany.net/multisite_files/coexist/imagecache/1280/poster/2013/04/1681873-poster-1280-stock-chart.jpg",
        "Children" : [ 
            {
                "_id" : ObjectId("5774464608b8f7f41a00004a"),
                "Name" : "Stock Software",
                "URLName" : "Stock-Software",
                "Image" : "http://s1.evcdn.com/images/block/I0-001/026/278/060-2.jpeg_/1-day-introduction-forex-stock-market-trading-60.jpeg",
                "Description" : "Stock Excba ge Software",
                "NoForums" : 0,
                "NoTopics" : 0,
                "NoComments" : 0,
                "NoUsers" : 0,
                "CoverImage" : "http://media.therakyatpost.com/wp-content/uploads/2015/04/stock-exchange-record-bigstock-900x470.jpg",
                "Children" : []
            }, 
            {
                "_id" : ObjectId("5774e2cb08b8f76c1a00002e"),
                "Name" : "Meta Trader",
                "URLName" : "Meta-Trader",
                "Image" : "http://www.file-extensions.org/imgs/app-icon/128/6904/metatrader-icon.png",
                "Description" : "MetaTrader",
                "NoForums" : 0,
                "NoTopics" : 0,
                "NoComments" : 0,
                "NoUsers" : 0,
                "CoverImage" : "http://www.roboforex.com/files/filemanager/image/site/metatrader.png",
                "Children" : []
            }, 
            {
                "_id" : ObjectId("5774eb2708b8f76c1a000043"),
                "Name" : "Meta Trader",
                "URLName" : "Meta-Trader",
                "Image" : "http://www.file-extensions.org/imgs/app-icon/128/6904/metatrader-icon.png",
                "Description" : "MetaTrader",
                "NoForums" : 0,
                "NoTopics" : 0,
                "NoComments" : 0,
                "NoUsers" : 0,
                "CoverImage" : "http://www.roboforex.com/files/filemanager/image/site/metatrader.png",
                "Children" : []
            }, 
            {
                "_id" : ObjectId("5774eb8208b8f76c1a000044"),
                "Name" : "Meta Trader",
                "URLName" : "Meta-Trader",
                "Image" : "http://www.file-extensions.org/imgs/app-icon/128/6904/metatrader-icon.png",
                "Description" : "MetaTrader",
                "NoForums" : 0,
                "NoTopics" : 0,
                "NoComments" : 0,
                "NoUsers" : 0,
                "CoverImage" : "http://www.roboforex.com/files/filemanager/image/site/metatrader.png",
                "Children" : []
            }, 
            {
                "_id" : ObjectId("5774eb8f08b8f76c1a000045"),
                "Name" : "Meta Trader",
                "URLName" : "Meta-Trader",
                "Image" : "http://www.file-extensions.org/imgs/app-icon/128/6904/metatrader-icon.png",
                "Description" : "MetaTrader",
                "NoForums" : 0,
                "NoTopics" : 0,
                "NoComments" : 0,
                "NoUsers" : 0,
                "CoverImage" : "http://www.roboforex.com/files/filemanager/image/site/metatrader.png",
                "Children" : []
            }
        ]
    }
],
"URLName" : "Business"
}

如何在Business.Children.Children中插入另一个文档。(股票软件).Children

1 个答案:

答案 0 :(得分:1)

使用位置运算符推送值。

https://docs.mongodb.com/manual/reference/operator/update/positional/

    db.collectioname.update(
     {
    "_id" : ObjectId("576bddaa08b8f7dc20000033"),
    "Children._id" : ObjectId("57715e0608b8f7441b00002d")
    },
    {
    $push:
   {"Children.$.Children":
     {
      "Name" : "Business School",
            "URLName" : "Business-School",
            "Image" : "http://www.small-business-website.net/wp-content/uploads/2016/04/How-to-achieve-a-successful-business-sale-128x128.jpg",
            "Description" : "Everything about Business Schools",
            "NoForums" : 0,
            "NoTopics" : 0,
            "NoComments" : 0,
            "NoUsers" : 0
       }
    }
   }
    )

注意:注意Mongo不允许嵌套文档更新。这是一个已知问题。它只允许一个级别。 https://jira.mongodb.org/browse/SERVER-831