瀏覽代碼

there is absolutely no need to use a where condition here

a regex will use the index, a where condition will scan the whole
collection
AD7six 13 年之前
父節點
當前提交
96550fd512
共有 1 個文件被更改,包括 2 次插入4 次删除
  1. 2 4
      lib/Gedmo/Sluggable/Mapping/Event/Adapter/ODM.php

+ 2 - 4
lib/Gedmo/Sluggable/Mapping/Event/Adapter/ODM.php

@@ -31,9 +31,7 @@ final class ODM extends BaseAdapterODM implements SluggableAdapter
         if ($identifier) {
             $qb->field($meta->identifier)->notEqual($identifier);
         }
-        $qb->where("function() {
-            return this.{$config['slug']}.indexOf('{$slug}') === 0;
-        }");
+        $qb->field($config['slug'])->Equals(new \MongoRegex('/^' . str_replace('/', '\/', $slug) . '/'));
         $q = $qb->getQuery();
         $q->setHydrate(false);
 
@@ -113,4 +111,4 @@ final class ODM extends BaseAdapterODM implements SluggableAdapter
             }
         }
     }
-}
+}