简体   繁体   中英

MongoDB and Doctrine. How to search text in array?

Some mongo document User contains the array of embedded document Message which has some sting member 'text':

/**
 * @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();

}

How to serach messages.text of all the users in datbaase for some sub-string? So if someone user has the message 'Hello Bob!', that one will be found by searching for 'Bob'.

Did you try something like:

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

Remember to use a fulltext index if you want some performances on this kind of request!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM