0

i have the following strucure:

{
'name':'something',
'commens':{
            'value':'something'
           }, {
            'value':'something else'
           }
}

My question is, how can i insert/update those subdocuments?

1
  • 1
    Can you be a bit more specific? Are you using a programming language, or are you referring to the MongoDB shell? Commented May 9, 2013 at 15:26

2 Answers 2

1

If you are using the MongoDB console, you will have to use the $ positional operator to update embedded documents.

db.yourCollection.update({ "_id" : ObjectId("4a33289ae89489"), "commens._id" : ObjectId("32321eae20fc603aee49124") }, { "$set" : { "commens.$.value" : "something else" } })
Sign up to request clarification or add additional context in comments.

Comments

0

I am assuming 'comments' is an array, otherwise the example you posted in not a valid JSON/BSON. For array operations, you can look : http://docs.mongodb.org/manual/reference/operator/update-array/

In update query only, you can do upsert, which will insert the document if it don't exists.

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.