Optimize array query match with operator $all in MongoDb

ⅰ亾dé卋堺 提交于 2020-01-05 15:05:30

问题


In a collection of 130k elements with the structure:

{
  "tags": ["restaurant", "john doe"]
}

There are 40k documents with "restaurant" tag but only 2 with "john doe". So the next queries are different:

// 0.100 seconds (40.000 objects scanned)
{"tags": {$all: [/^restaurant/, /^john doe/]}}

// 0.004 seconds (2 objects scanned)
{"tags": {$all: [/^john doe/, /^restaurant/]}}

It's there a way to optimize the query without sorting the tags in the client? The only way I can imagine now is putting less frequent tags at start of the search array.


回答1:


I found a request feature for this in mongodb team JIRA:

https://jira.mongodb.org/browse/SERVER-1000

I implemented a stadistic system to put tags with more cadinality at the end of the array.



来源:https://stackoverflow.com/questions/12416173/optimize-array-query-match-with-operator-all-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!