2

I want just push object into array of objects in mongodb

 {
    "_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 to push object into above document in need to do

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

So how can i implement same functionality using mgo driver

1

2 Answers 2

2

Try the following:

session, err := mgo.Dial("127.0.0.1")
if err != nil {
    panic(err)
}

defer session.Close()

session.SetMode(mgo.Monotonic, true)

// Drop Database
if IsDrop {
    err = session.DB("test").DropDatabase()
    if err != nil {
        panic(err)
    }
}

// Collection Stack
c := session.DB("test").C("stack")

// Query
query := bson.M{"ownerEmail": "[email protected]"}
update := bson.M{"$push": bson.M{"camps": bson.M{"name": "cubs-killeen", "location": "some other Place"}}}

// Update
err = c.Update(query, update)
if err != nil {
    panic(err)
}
Sign up to request clarification or add additional context in comments.

5 Comments

thanx for clarified ans
@AkashShinde No worries :-)
above method is adding element as array
@AkashShinde I thought in your question that's the update you wanted. The $push operator appends a specified value to an array. Consider using the $addToSet operator, it adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array.
sorry your code working fine, i did mistake , i was appending array to array
2

I assume your codebase has Camps struct similar to:

type Camps struct {
    Name     string   `json:"name,omitempty"`
    Location string   `json:"location,omitempty"`
 }

Golang code:

database := "yourDatabaseName"  
collection := "stack"

session, err := mgo.Dial("127.0.0.1")
if err != nil {
    panic(err)
 }
defer session.Close()

session.SetMode(mgo.Monotonic, true)
c := session.DB(database).C(collection)

data := model.Camps{
        Name:       "cubs-killeen",
        Location:   "some other Place",
   }
selector := bson.M{"ownerEmail": "[email protected]"}
changes := bson.M{"$push": bson.M{"camps": bson.M{"$each": []model.Camps{data}}}}
err = c.Update(selector, changes)

if err != nil {
    panic(err)
 }

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.