MongoDB和Doctrine.如何在数组中搜索文本


MongoDB and Doctrine. How to search text in array?

一些 mongo 文档 用户包含嵌入文档的消息数组,该消息具有一些刺痛成员"文本":

/**
 * @MongoDB'EmbeddedDocument
 */
class Message {
     /**
     * @MongoDB'Field(type="string")
     */
    protected $text;
}
/**
 * @MongoDB'Document(collection="chats")
 * @JMS'ExclusionPolicy("none")
 */
class User {
     /**
     * @MongoDB'EmbedMany(targetDocument=myBundle'Document'Message", strategy="addToSet")
     */
    protected $messages = array();
}

如何在 datbaase 中为某些子字符串搜索所有用户的消息.文本?因此,如果某人用户有消息"你好鲍勃!",将通过搜索"鲍勃"找到该消息。

您是否尝试过类似的东西:

$product = $this->get('doctrine_mongodb')
    ->getManager()
    ->createQueryBuilder('YourOwnBundle:User')
    ->field('messages.text')->equals(new 'MongoRegex('Bob'))
;

如果您想在这种请求上获得一些性能,请记住使用全文索引!