MongoDB中的复合索引+正则表达式

时间:2014-08-05 21:20:31

标签: regex mongodb indexing

我正在尝试使用复杂的mongoDB索引来解决一个简单的问题。我有一个events集合和一个participants集合。 participants集合中的文档具有searchable_keywords字段,该字段是数组。

我的guests集合中有以下复合索引:

event_id_1_searchable_keywords_1

来自我的公寓我在event_id集合上有一个简单的索引guests字段。

当我运行以下查询时:

eventId = db.events.findOne()["_id"];
db.guests.find({"event_id": eventId, "searchable_keywords": "engineer"}).explain()

我可以看到正在使用正确的索引。

问题是当我使用正则表达式搜索运行它时:

db.guests.find({"event_id": eventId, "searchable_keywords": /^engin/}).explain(true)

结果如下:

{
  "cursor" : "BtreeCursor event_id_1",
  "isMultiKey" : false,
  "n" : 0,
  "nscannedObjects" : 11,
  "nscanned" : 11,
  "nscannedObjectsAllPlans" : 40,
  "nscannedAllPlans" : 48,
  "scanAndOrder" : false,
  "indexOnly" : false,
  "nYields" : 0,
  "nChunkSkips" : 0,
  "millis" : 1,
  "indexBounds" : {
    "event_id" : [
        [
            ObjectId("532c5a5887512e567a00067a"),
            ObjectId("532c5a5887512e567a00067a")
        ]
    ]
},
"allPlans" : [
    {
        "cursor" : "BtreeCursor event_id_1",
        "isMultiKey" : false,
        "n" : 0,
        "nscannedObjects" : 11,
        "nscanned" : 11,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nChunkSkips" : 0,
        "indexBounds" : {
            "event_id" : [
                [
                    ObjectId("532c5a5887512e567a00067a"),
                    ObjectId("532c5a5887512e567a00067a")
                ]
            ]
        }
    },
    {
        "cursor" : "BtreeCursor deleted_at_1_event_id_1_updated_at_1_created_at_-1",
        "isMultiKey" : false,
        "n" : 0,
        "nscannedObjects" : 11,
        "nscanned" : 11,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nChunkSkips" : 0,
        "indexBounds" : {
            "deleted_at" : [
                [
                    null,
                    null
                ]
            ],
            "event_id" : [
                [
                    ObjectId("532c5a5887512e567a00067a"),
                    ObjectId("532c5a5887512e567a00067a")
                ]
            ],
            "updated_at" : [
                [
                    {
                        "$minElement" : 1
                    },
                    {
                        "$maxElement" : 1
                    }
                ]
            ],
            "created_at" : [
                [
                    {
                        "$maxElement" : 1
                    },
                    {
                        "$minElement" : 1
                    }
                ]
            ]
        }
    },
    {
        "cursor" : "BtreeCursor event_id_1_searchable_keywords_1_created_at_-1",
        "isMultiKey" : true,
        "n" : 0,
        "nscannedObjects" : 9,
        "nscanned" : 13,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nChunkSkips" : 0,
        "indexBounds" : {
            "event_id" : [
                [
                    ObjectId("532c5a5887512e567a00067a"),
                    ObjectId("532c5a5887512e567a00067a")
                ]
            ],
            "searchable_keywords" : [
                [
                    "",
                    {

                    }
                ],
                [
                    /engin/,
                    /engin/
                ]
            ],
            "created_at" : [
                [
                    {
                        "$maxElement" : 1
                    },
                    {
                        "$minElement" : 1
                    }
                ]
            ]
        }
    },
    {
        "cursor" : "BtreeCursor deleted_at_1_event_id_1_searchable_keywords_1_created_at_-1",
        "isMultiKey" : true,
        "n" : 0,
        "nscannedObjects" : 9,
        "nscanned" : 13,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nChunkSkips" : 0,
        "indexBounds" : {
            "deleted_at" : [
                [
                    null,
                    null
                ]
            ],
            "event_id" : [
                [
                    ObjectId("532c5a5887512e567a00067a"),
                    ObjectId("532c5a5887512e567a00067a")
                ]
            ],
            "searchable_keywords" : [
                [
                    "",
                    {

                    }
                ],
                [
                    /engin/,
                    /engin/
                ]
            ],
            "created_at" : [
                [
                    {
                        "$maxElement" : 1
                    },
                    {
                        "$minElement" : 1
                    }
                ]
            ]
        }
    }
],
"server" : "Romains-MacBook-Pro-2.local:27017",
"filterSet" : false,
"stats" : {
    "type" : "KEEP_MUTATIONS",
    "works" : 13,
    "yields" : 0,
    "unyields" : 0,
    "invalidates" : 0,
    "advanced" : 0,
    "needTime" : 11,
    "needFetch" : 0,
    "isEOF" : 1,
    "children" : [
        {
            "type" : "FETCH",
            "works" : 12,
            "yields" : 0,
            "unyields" : 0,
            "invalidates" : 0,
            "advanced" : 0,
            "needTime" : 11,
            "needFetch" : 0,
            "isEOF" : 1,
            "alreadyHasObj" : 0,
            "forcedFetches" : 0,
            "matchTested" : 0,
            "children" : [
                {
                    "type" : "IXSCAN",
                    "works" : 11,
                    "yields" : 0,
                    "unyields" : 0,
                    "invalidates" : 0,
                    "advanced" : 11,
                    "needTime" : 0,
                    "needFetch" : 0,
                    "isEOF" : 1,
                    "keyPattern" : "{ event_id: 1 }",
                    "boundsVerbose" : "field #0['event_id']: [ObjectId('532c5a5887512e567a00067a'), ObjectId('532c5a5887512e567a00067a')]",
                    "isMultiKey" : 0,
                    "yieldMovedCursor" : 0,
                    "dupsTested" : 0,
                    "dupsDropped" : 0,
                    "seenInvalidated" : 0,
                    "matchTested" : 0,
                    "keysExamined" : 11,
                    "children" : [ ]
                }
            ]
        }
    ]
  }
}

是否可以在复合索引上对MongodDB执行正则表达式搜索?

0 个答案:

没有答案