Ayy*_*a A 3 mapreduce mongodb node.js sails.js
I am looking for a logic to retrieve data from database from 1000s records. I cant do at application level.
I have data with ending two twin letter like "ll, gg, ss, ff... ". wants to retrieve words which ends with the above twin characters from DB.
My sample DB:
[{
"word": "Floss"
}, {
"word": "smacx"
}, {
"word": "fuzz"
}, {
"word": "grass"
}, {
"word": "dress"
}, {
"word": "puff"
}, {
"word": "cliff"
}, {
"word": "sniff"
}, {
"word": "chess"
}, {
"word": "kiss"
}, {
"word": "fell"
}, {
"word": "shell"
}]
Run Code Online (Sandbox Code Playgroud)
checkarray = ['ll','gg','ll','ss'];
Any idea on how to do this as DB Level. Application Level looping is making high and taking more time as it has almost 100k records.
您可以通过创建一个新的RegExp对象数组来将 $in 与正则表达式一起使用,如下所示:$in
var checkarray = ['ll','gg','ll','ss'],
regex = checkarray.map(function (k) { return new RegExp(k); });
db.collection.find({
"word": { "$in": regex }
})
Run Code Online (Sandbox Code Playgroud)
请记住,使用$in对于小数组可能相当有效,但对于大列表则不太有效,因为它会在索引中跳过以查找匹配的文档,或者如果没有要使用的索引,则遍历整个集合。
除了将 $in 与正则表达式一起使用外,您还可以使用$regex带有管道分隔正则表达式模式的运算符,其中包含 checkarray,如下所示:
var checkarray = ['ll','gg','ll','ss'],
regex = checkarray.join("|");
db.collection.find({
"word": {
"$regex": regex,
"$options": "i"
}
})
Run Code Online (Sandbox Code Playgroud)
要匹配最后两个字符,请使用以下模式\gg$\,即附加$到模式,其中$元字符表示字符串的结尾。例如,模式abc$可以匹配以下内容 abc, endsinabc, 123abc, ...。
所以,对于你的后续问题
我需要以 checkArray 字母结尾的单词,而不是中间或开头的单词。CheckArray 字符应在字符串的结尾字母中。像“鸡蛋”而不是“填充”
你可以这样做:
var checkarray = ['ll','gg','ff','ss'],
regex = checkarray.map(function (k) { return new RegExp(k+'$'); });
db.collection.find({
"word": { "$in": regex }
})
Run Code Online (Sandbox Code Playgroud)
要对此进行测试,请填充这些示例文档以测试集合:
db.test.insert([
{ "_id": 1, "word" : "well" },
{ "_id": 2, "word" : "filled" },
{ "_id": 3, "word" : "glass" },
{ "_id": 4, "word" : "blessed" }
])
Run Code Online (Sandbox Code Playgroud)
上面的查询将返回带有_ids 1 和 3 的文档。
{ "_id" : 1, "word" : "well" }
{ "_id" : 3, "word" : "glass" }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2802 次 |
| 最近记录: |