Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDb : How to insert additional object into object collection?

I have a document "owner" that can have "n" number of camps and camps have "instructors" and instructors have "classes". Earlier I tried accomplishing this with nested arrays (see link to my post below), however I learned that the positional operator "$" does not nest that deep. MongoDB: Adding an array into an existing array

However, I learned that the workaround would be to use object collections instead of arrays. I'm assuming that I have to use "update" with "$set" to add additional "camps", however every time I do all it does is overwrite(update) the previous camp that was inserted.

Below is the hierarchical structure I am trying to accomplish:

owner = {

    firstName: 'john',
    lastName: 'smith',
    ownerEmail: '[email protected]',

    camps : {

        {
            name: 'cubs-killeen',
            location: 'killeen'
        },

        {
            name: 'cubs-temple',
            location: 'temple'
        },

        instructors : {

            {
                firstName: 'joe',
                lastName : 'black'
            },

            {
                firstName: 'will',
                lastName : 'smith'
            }        
        }

    }

}

I have also been trying the following:

db.owners.update({ownerEmail:'[email protected]'}, {$set: { camps:{ {name:'cubs-killeen'} } }})

but this throws an unexpected identifier { error.

Any help with some sample mongo commands to achieve the structure above would be most appreciated.

V/R

Chris

like image 515
cpeele00 Avatar asked Jun 25 '13 02:06

cpeele00


People also ask

How do I add data to a collection in MongoDB?

In MongoDB, the insert() method inserts a document or documents into the collection. It takes two parameters, the first parameter is the document or array of the document that we want to insert and the remaining are optional. Using this method you can also create a collection by inserting documents.

How can I insert a new field in an existing MongoDB collection document?

To add field or fields to embedded documents (including documents in arrays) use the dot notation. See example. To add an element to an existing array field with $addFields , use with $concatArrays .

What function inserts multiple documents into a collection?

Given an array of documents, insertMany() inserts each document in the array into the collection.


1 Answers

As other mentioned, The structure you want is not valid. I recommend the following structure for your owner document:

    {
    "_id" : ObjectId("51c9cf2b206dfb73d666ae07"),
    "firstName" : "john",
    "lastName" : "smith",
    "ownerEmail" : "[email protected]",
    "camps" : [
            {
                    "name" : "cubs-killeen",
                    "location" : "killeen"
            },
            {
                    "name" : "cubs-temple",
                    "location" : "temple"
            }
    ],
    "instructors" : [
            {
                    "firstName" : "joe",
                    "lastName" : "black"
            },
            {
                    "firstName" : "will",
                    "lastName" : "smith"
            }
    ]
}

and then

db.stack.update(
  { ownerEmail: "[email protected]" },
  {
    $push: {
      camps: { name: "cubs-killeen", location: "some other Place" }
    }
  }
);

Having this, you can add camps like this:

Hope it helps.

like image 123
AntonioOtero Avatar answered Sep 21 '22 11:09

AntonioOtero