简体   繁体   中英

Doctrine Mongodb ODM elemMatch()

I have a collection of categories as below.

 {
   "_id": ObjectId("5353a17dfcda358c1050d1ed"),
   "name": "Motorcyles",
   "tags": {"Vehicle","two wheeler","Bikes"}
 },
 {
   "_id": ObjectId("5353a17dfcda358c105bde4c"),
   "name": "Automobiles",
   "tags": {"Vehicle","Cars","Bikes", "two wheeler", "four wheeler"}
 },
 {
   "_id": ObjectId("5353a17dfcda358c105fd5e701),
   "name": "Cars",
   "tags": {"Vehicle","Cars","four wheeler"}
 }

Now I want to list the categories using doctrine ODM query builder and filter categories matching name or values stored in tags. (ie when search text is car then the result should be two categories - Automobiles and Cars as Automobiles has cars in tag and there is a category with name Cars).

I hope you understood the situation.

I am using Symfony2 and doctrine-odm. I have tried using elemMatch() but I could not find the solution. I got the solutions for only the indexed array with key and value. But In my case, tags contains the array with out keys.

Could anyone please help.

If tags is just an array, you could just consider it as a text field in your query. You could do this:

$qb = $this->createQueryBuilder('App:Something');
$qb->field('tags')->equals('Cars');

It will then search in tags.

In your case you might want a regular expression and you might want to use an "or" operator to search either of the fields. In that case you have to use addOr().

$qb->addOr($qb->expr()->field('tags')->equals(new \MongoRegex("/Cars/i")));
$qb->addOr($qb->expr()->field('name')->equals(new \MongoRegex("/Cars/i")));

Examples can be found at http://www.jamiesutherland.com/2011/07/01/doctrine-2-odm-querybuilder-addor/ and MongoDB/doctrine: can't nest $or in $and

Note that this is a performance killer because of the regexes. Also note that if you don't use regexes but don't define a composite index on name, tags , it will also be a performance killer. It needs to be one index on both the fields, defined in the top definition of your Doctrine Document, so before "class Something".

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