2

I'm using Python and Mongo for the fist time together and in documentation I wasn't able to find what I need.

So my data object looks like this

{
"_id" : ObjectId("54d372597d74523bc6991b9b"),
"id_user" : "2000001",
"date_registrated" : "2015-01-21 12:11:28.185",
"user" : "Bogdan",
"gender" : "M",
"email" : "[email protected]",
"charachters" : [ 
    {
        "quest_info" : "TUT_var,1421842359 STARTAREA,4 ",
        "char_name" : "Testarion"
    }
]
}

And I want to add new field into existing charachters, something like

party_user = {"party_name": "name",
               "admin": 0}

And finally I want to get this:

{
"_id" : ObjectId("54d372597d74523bc6991b9b"),
"id_user" : "2000001",
"date_registrated" : "2015-01-21 12:11:28.185",
"user" : "Bogdan",
"gender" : "M",
"email" : "[email protected]",
"charachters" : [ 
    {
        "quest_info" : "TUT_var,1421842359 STARTAREA,4 ",
        "char_name" : "Testarion"
        **"parties" : [{party 1},{party 2}]**
    }
]
}

The problem is how to create query that makes that ? I've tried with something like this but it failed miserably:

db.collection('MyDB').update(
                {"char_name": "Testarion"},
                {"$push": {
                    "charachters": {"parties": party_user}
                }})

I'm still new with Mongo and haven't catched up all the things but can you please show me what am I doing wrong ? Is that even possible ?

1 Answer 1

3

Use '$' in your update query for that array element:

db.collection.update(
   {"charachters.char_name": "Testarion"},
   {"$push": {"charachters.$.parties": "party_user"}})
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.