我们需要帮助来构建我们的Mongo数据库。我们需要存储国家ID,然后运行查询以返回包含匹配国家的文档。假设ID是6-10个字符的字符串。
有两个选项:
1)将国家/地区ID存储为一个由分隔符分隔的大字符串
(例如,/)。Ex: "IDIDID1/IDIDID2/IDIDID3/IDIDID4/IDIDID5".
2)将in存储在数组中。
Ex: ["IDIDID1", "IDIDID2", "IDIDID3", "IDIDID4", "IDIDID5"].我们希望针对"Find all documents containing country ID IDIDID3.“这样的查询进行优化
对于选项1,我们计划使用RegEx来查询文档(例如,/IDIDID3/)。
对于选项2,我们将使用标准$in运算符。
哪个选项会产生更好的读取性能?
使用字符串方法是否会产生更好的性能,因为您可以索引字符串(与Mongo只能索引一个数组的限制相反)?
我们正在使用MongoMapper。
发布于 2013-03-26 18:10:12
摘自MongDB手册
$regex can only use an index efficiently when the regular expression
has an anchor for the beginning (i.e. ^) of a string and is a case-sensitive match.
Additionally, while /^a/, /^a.*/, and /^a.*$/ match equivalent strings,
they have different performance characteristics.
All of these expressions use an index if an appropriate index exists;
however, /^a.*/, and /^a.*$/ are slower. /^a/ can stop scanning after matching the prefix.因此,使用数组和多键索引在性能方面更有意义
https://stackoverflow.com/questions/15631924
复制相似问题