3

我在 MongoDB 中有一个名为 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"

                      }
                     ]
       }

我想在 sender_id=5 的提案属性中添加消息,如下所示;

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"

                          }
                         ]
           }

我的文件是,

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

我真的很难做这件事。这是向提案数组添加消息。php代码有什么帮助吗?

4

2 回答 2

2

这对我来说是完美的答案,

db.jobs.update({"_id" : "12344","Proposals.sender_id":"5"},{$push:{"Proposals.$.messages" : { "message" : "Can we meet" , "date":"2013-08-06"}}})
于 2013-10-01T10:20:05.580 回答
0

通常,当您遇到这样的复杂情况时,这意味着您需要将子文档拆分为真正的顶级文档 - 可能拆分为不同的集合。在这种情况下,您肯定需要将提案推送到他们自己的集合中,并且您需要更改您的 _id 并添加一个 job_id 以引用真实的工作:

{
    "_id" => …,
    "job_id" => 12344,
    "sender_id" => "5",
    "description" =>"I can do your task before 2 week", 
    "date" => "2013-08-05"
},

{
    "_id" => …,
    "job_id" => 12344,
    "sender_id" => "6",
    "description" =>"I can do your task in 2 week, with best quality", 
    "date" => "2013-08-05"
}

然后,您可以使用以下命令将新消息推送到数组中:

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

$collection->update(
    array( 'job_id' => 12344, 'sender_id' => "5" ),
    array( 'messages' => array( '$push' => $document ) )
);

确保你也创建了一个索引job_id, sender_id

$collection->ensureIndex( array( 'job_id' => 1, "sender_id" => 1 ) );
于 2013-08-08T10:11:12.243 回答