Chi Haha
Chi Haha

Reputation: 43

how do add/push given fields in mongodb?What will be the mondodb query?

So this is my collection snippet:

{
        "_id" : ObjectId("56d82c76c07d41a38d418120"),
        "userid" : "a",
        "dates" : {
                "2/01/2015" : {
                        "9/10" : {
                                "ava" : "yes",
                                "bookibg_id" : "null"
                        },
                        "10/11" : {
                                "ava" : "yes",
                                "bookibg_id" : "null"
                        }
                },
                "3/01/2015" : {
                        "9/10" : {
                                "ava" : "yes",
                                "bookibg_id" : "null"
                        },
                        "10/11" : {
                                "ava" : "yes",
                                "bookibg_id" : "null"
                        }
                }}}
{
        "_id" : ObjectId("56d82c76c07d41a38d418120"),
        "userid" : "b",
        "dates" : {
                "2/01/2015" : {
                        "9/10" : {
                                "ava" : "yes",
                                "bookibg_id" : "null"
                        },
                        "10/11" : {
                                "ava" : "yes",
                                "bookibg_id" : "null"
                        }
                },
                "3/01/2015" : {
                        "9/10" : {
                                "ava" : "yes",
                                "bookibg_id" : "null"
                        },
                        "10/11" : {
                                "ava" : "yes",
                                "bookibg_id" : "null"
                        }
                }}}

I want to add/push given given snippet into user id:b ,dates fiels: Snippet:

"4/01/2015" : {
                        "9/10" : {
                                "ava" : "yes",
                                "bookibg_id" : "null"
                        },
                        "10/11" : {
                                "ava" : "yes",
                                "bookibg_id" : "null"
                        }
                } 

So I want to add the snippet dates>snippet where userid=b What will be the mongodb query? I searched about $push on mongo docs but it says that the field needs to be an array otherwise it wont happen.

Upvotes: 4

Views: 53

Answers (2)

Gaurav joshi
Gaurav joshi

Reputation: 1799

No, you cannot do this kind of things

for this you have to find the document then add new value in the object then update the object

Upvotes: 0

Ahmed H. Gomaa
Ahmed H. Gomaa

Reputation: 136

Can you try this code:

db.dates.update({"userid":"b"}, {$set:{"dates.4/01/2015":{
                    "9/10" : {
                            "ava" : "yes",
                            "bookibg_id" : "null"
                    },
                    "10/11" : {
                            "ava" : "yes",
                            "bookibg_id" : "null"
                    }
            } }})

Upvotes: 1

Related Questions