1

I am looking to insert another document in a multiple nested document. Do you have any other ideas of how to solve it? Thanks

{
"_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"
}

How can I insert another document in Business.Children.Children.(Stock Software).Children

1 Answer 1

1

Use the positional operator to push values.

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
      }
    }
  }
)

Note : Beware Mongo does not not allow nested documents to update.It is a known issue. It allows only to a single level. https://jira.mongodb.org/browse/SERVER-831

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.