1

我在 MongoDB 中有一个名为 topic 的集合

topic{
    topic_title: 'thread',
    reply{
        reply_title: 'thread',
        reply_content: 'content'
        reply_created: "2013-06-18 17:54:04" 
    }

}

我想添加对该主题的回复,例如:

topic{
    topic_title: 'thread',
    reply{
        reply_title: 'thread',
        reply_content: 'content'
        reply_created: "2013-06-18 17:54:04" 
    }
    {
        reply_title: 'reply1',
        reply_content: 'reply2'
        reply_created: "2013-06-18 17:57:04" 
            }
}

我的代码是这样的:

   $reply = array(
                            "reply_title" => $title,
                            "reply_content" =>$content,
                            "reply_created" => date('Y-m-d H:i:s')
        );
    $document = array('$push' => array("reply" => $reply));

    $id = new MongoId($topicid);
    $topic->update(array("_id"=>$id),$document);

它做了这样的事情

     topic{
           topic_title: 'replytitle'
           reply {
                    reply_title: "replytitle"
                    reply_content: "replycontent"
                    reply_created: "2013-06-18 17:57:12"
                 }

          }
      reply{
            reply{
                    reply_title: "replytitle"
                    reply_content: "replycontent"
                    reply_created: "2013-06-18 17:57:12"
             }
            reply{
                    reply_title: "replytitle"
                    reply_content: "replycontent"
                    reply_created: "2013-06-18 17:57:12"
             }
           }

我真的很难做一件简单的事情。这是向回复数组添加回复。有什么帮助吗?

4

1 回答 1

0

我有点不清楚您的文档结构是什么,因为您发布的不是有效的 JSON。但是假设它topic.reply应该是一个文档数组,您可以$push像这样使用运算符:

$topic->update( array( "_id" => $id ), array( "$push" => array( "topic.reply", $reply ) ) );
于 2013-06-18T15:13:08.547 回答