我想用Doctrine MongoDB ODM获取这个mongodb命令的结果:
db.posts.find({_id: 3}, {comments: {$slice: [0, 5]}})
我一直在寻找它,但在文档中都找不到任何东西。如果您有任何想法,请帮助,谢谢。
以下是我的文档类:
/** @ODM\Document(db="dbname" collection="posts") */
class Posts{
/** @ODM\Id */
private $_id;
/** @ODM\Field(type="string") */
private $some_field;
/** @ODM\EmbedMany(targetDocument="Comment") */
private $comments = array();
public function getSomeField() { return $this->some_field; }
public function setSomeField($content) { $this->some_field = $content; }
public function getComments() { return $this->comments; }
public function addComment(Comment $comment) { $this->comments[] = $comment; }
}
/** @ODM\EmbeddedDocument */
class Comment {
/** @ODM\Field(type="string") */
private $timeStamp;
/** @ODM\Field(type="string") */
private $message;
/** @ODM\ReferenceOne(targetDocument="Users", simple=true) */
private $user_ref;
public function getTimeStamp() { return $this->timeStamp; }
public function setTimeStamp($str) { $this->timeStamp = $str;}
public function getMessage() { return $this->message; }
public function setMessage($str) { $this->message = $str; }
public function getUser() { return $this->user_ref; }
public function setUser(Users $user) { $this->user_ref = $user; }
}
答案 0 :(得分:0)
虽然有关查询构建器->select()
的文档并未详细介绍细节或方法,但以下代码将允许您运行命令:
$qb = $this->dm->createQueryBuilder(Posts::class)
->selectSlice('comments', 0, 5)
->field('_id')->equals(3);
$query = $qb->getQuery();
$results = $query->execute();