如何在mongodb中搜索逗号分隔的数据 [英] How to search comma separated data in mongodb
问题描述
我有不同字段的电影数据库.类型字段包含逗号分隔的字符串,例如:
I have movie database with different fields. the Genre field contains a comma separated string like :
{genre: 'Action, Adventure, Sci-Fi'}
我知道我可以使用正则表达式来查找匹配项.我也尝试过:
I know I can use regular expression to find the matches. I also tried:
{'genre': {'$in': genre}}
问题是运行时间.返回查询结果需要花费大量时间.数据库中大约有30万个文档,我已经对体裁"字段进行了正常索引.
the problem is the running time. it take lot of time to return a query result. the database has about 300K documents and I have done normal indexing over 'genre' field.
推荐答案
Would say use Map-Reduce to create a separate collection that stores the genre
as an array with values coming from the split comma separated string, which you can then run the Map-Reduce job and administer queries on the output collection.
例如,我已经为foo
集合创建了一些示例文档:
For example, I've created some sample documents to the foo
collection:
db.foo.insert([
{genre: 'Action, Adventure, Sci-Fi'},
{genre: 'Thriller, Romantic'},
{genre: 'Comedy, Action'}
])
然后,下面的map/reduce操作将产生一个集合,您可以从中应用性能查询:
The following map/reduce operation will then produce the collection from which you can apply performant queries:
map = function() {
var array = this.genre.split(/\s*,\s*/);
emit(this._id, array);
}
reduce = function(key, values) {
return values;
}
result = db.runCommand({
"mapreduce" : "foo",
"map" : map,
"reduce" : reduce,
"out" : "foo_result"
});
查询将非常简单,将查询与value
字段上的多键索引结合使用:
Querying would be straightforward, leveraging the queries with an multi-key index on the value
field:
db.foo_result.createIndex({"value": 1});
var genre = ['Action', 'Adventure'];
db.foo_result.find({'value': {'$in': genre}})
输出:
/* 0 */
{
"_id" : ObjectId("55842af93cab061ff5c618ce"),
"value" : [
"Action",
"Adventure",
"Sci-Fi"
]
}
/* 1 */
{
"_id" : ObjectId("55842af93cab061ff5c618d0"),
"value" : [
"Comedy",
"Action"
]
}
这篇关于如何在mongodb中搜索逗号分隔的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!