Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

push array within array within array(collection) in mongodb using php

Tags:

php

mongodb

I have a collection in MongoDB called job

    Job{
           _id : ‘12344’,
           cust_id: 'cust1',
           title: 'Create Website',
           description: 'We require it in 2 weeks only',
           location: 'japan',
           images:{
                      'image1',
                      'image2',
                      'image3' 
                  },
            video:{
                      'video1',
                      'video2'
                  },
           budget:'15000',
           duration:'2 weeks',
           Proposals:[{
                           "sender_id" => "5",
                           "description" =>"I can do your task before 2 week", 
                           "date" => "2013-08-05"
                      },
                      {
                            "sender_id" => "6",
                            "description" =>"I can do your task in 2 week, with best quality", 
                            "date" => "2013-08-05"

                      }
                     ]
       }

I want to add messages in proposal attributes whose sender_id=5 like below;

Job{
               _id : ‘12344’,
               cust_id: 'cust1',
               title: 'Create Website',
               description: 'We require it in 2 weeks only',
               location: 'japan',
               images:{
                          'image1',
                          'image2',
                          'image3' 
                      },
                video:{
                          'video1',
                          'video2'
                      },
               budget:'15000',
               duration:'2 weeks',
               Proposals:[{
                               "sender_id" => "5",
                               "description" =>"I can do your task before 2 week", 
                            "date" => "2013-08-05"
                               “messages”:[{
                                         "message_sender" :"Can we meet", 
                                         "date" : "2013-08-06"  
                                       }
                                      ]
                          },
                          {
                                "sender_id" => "6",
                                "description" =>"I can do your task in 2 week, with best quality", 
                            "date" => "2013-08-05"

                          }
                         ]
           }

My document is,

$document = array( 
                    "message_sender" =>"Can we meet", 
                 "date" => "2013-08-06"
         );

I am really having a hard time doing a this thing. which is add a messages to the proposal array. Any help out there with php code?

like image 701
Amrut Gaikwad Avatar asked Feb 06 '26 20:02

Amrut Gaikwad


1 Answers

This is perfect answer work for me,

db.jobs.update({"_id" : "12344","Proposals.sender_id":"5"},{$push:{"Proposals.$.messages" : { "message" : "Can we meet" , "date":"2013-08-06"}}})
like image 175
Amrut Gaikwad Avatar answered Feb 09 '26 11:02

Amrut Gaikwad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!