如何使用Doctrine ODM更新MongoDB中的嵌入式文档 [英] How to update embedded document in MongoDB with Doctrine ODM

查看:127
本文介绍了如何使用Doctrine ODM更新MongoDB中的嵌入式文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Symfony2中找不到如何使用Doctrine Mongo ODM更新嵌入式文档。我有一个名为页面的类,其中包含许多嵌入式文档评论,我想使用createQueryBuilder更新具体的注释。这是一个简单的课程,我有:

I'm unable to find how to update embedded documents with Doctrine Mongo ODM in Symfony2. I have a class called Page with many embedded documents "Comments" and I want to use createQueryBuilder to update specific comment. Here is a simple class that I have:

class Page
{

class Page {

protected $id;

/** @MongoDB\EmbedMany */
private $pageComment = array();

}

我搜索了整个互联网,但是我没有看到有关如何使用Doctrine ODM查询构建器更新文档的子文档的任何信息。我会感谢任何信息,因为我是新教徒和蒙古人。简单来说,我想通过id来更新页面中的具体评论。

I searched the whole internet, but I don't see to find any info on how to update subdocuments of a document with Doctrine ODM query builder. I will be thankful for any information as I'm new to both Doctrine and Mongo. In simple words I want to update specific comment in a page after searching for it by id.

提前感谢您的帮助!

推荐答案

 $yourArrayPageComment = array(
  "id" => new \MongoId($pageCommentId),
  "field1" => $field1,
  ...
)

 $this->createQueryBuilder('page')
            ->update()
            ->field('id')->equals($pageId)
            ->field('pageComment.id')->equals($pageCommentId)
            ->field("pageComment.$")->set($yourArrayPageComment)
            ->getQuery()
            ->execute();

这篇关于如何使用Doctrine ODM更新MongoDB中的嵌入式文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆