Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 带数组的MongoDB正则表达式搜索_Node.js_Mongodb_Mapreduce_Sails.js - Fatal编程技术网

Node.js 带数组的MongoDB正则表达式搜索

Node.js 带数组的MongoDB正则表达式搜索,node.js,mongodb,mapreduce,sails.js,Node.js,Mongodb,Mapreduce,Sails.js,我正在寻找一种逻辑,从数据库中检索1000s记录中的数据。我在应用程序层面做不到 我有两个双字母结尾的数据,比如“ll,gg,ss,ff…”。希望从DB中检索以上述双字符结尾的单词 我的示例数据库: [{ "word": "Floss" }, { "word": "smacx" }, { "word": "fuzz" }, { "word": "grass" }, { "word": "dress" }, { "w

我正在寻找一种逻辑,从数据库中检索1000s记录中的数据。我在应用程序层面做不到

我有两个双字母结尾的数据,比如“ll,gg,ss,ff…”。希望从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"
  }]
checkarray=['ll','gg','ll','ss']

关于如何在DB级别执行此操作的任何想法。应用程序级循环越来越高,需要花费更多的时间,因为它有将近100k条记录。

您可以通过创建一个新的对象数组来使用表达式,如下所示:

var checkarray = ['ll','gg','ll','ss'],
    regex = checkarray.map(function (k) { return new RegExp(k); });
db.collection.find({
    "word": { "$in": regex } 
})
请记住,使用对于小数组非常有效,但对于大列表则不太有效,因为它会在索引中跳转以查找匹配的文档,或者在没有索引可使用的情况下遍历整个集合


此外,您可以将运算符与包含checkarray的管道分隔正则表达式模式一起使用,如下所示:

var checkarray = ['ll','gg','ll','ss'],
    regex = checkarray.join("|");
db.collection.find({
    "word": {
        "$regex": regex, 
        "$options": "i"
    } 
})
var checkarray = ['ll','gg','ff','ss'],
    regex = checkarray.map(function (k) { return new RegExp(k+'$'); });
db.collection.find({
    "word": { "$in": regex } 
})

要匹配最后两个字符,请使用以下模式
\gg$\
,即在
$
元字符表示字符串结尾的模式中附加
$
。例如,模式
abc$
可以匹配以下
abc,endsinabc,123abc,…

那么,关于你的后续问题

我需要以checkArray字母结尾的单词,而不是 中间或开始。CheckArray字符应位于结尾字母中 一根绳子。像“鸡蛋”而不是“填满”

你可以这样做:

var checkarray = ['ll','gg','ll','ss'],
    regex = checkarray.join("|");
db.collection.find({
    "word": {
        "$regex": regex, 
        "$options": "i"
    } 
})
var checkarray = ['ll','gg','ff','ss'],
    regex = checkarray.map(function (k) { return new RegExp(k+'$'); });
db.collection.find({
    "word": { "$in": regex } 
})
要对此进行测试,请填充以下示例文档以测试集合:

db.test.insert([
    { "_id": 1, "word" : "well" },
    { "_id": 2, "word" : "filled" },
    { "_id": 3, "word" : "glass" },
    { "_id": 4, "word" : "blessed" }
])
上述查询将返回id为1和3的文档

{ "_id" : 1, "word" : "well" }
{ "_id" : 3, "word" : "glass" }

令人惊叹的。工作很好,感谢您的快速响应。零钱。我需要在中间或开始时检查数组的字母结束。CheckArray字符应位于字符串的结尾字母中。喜欢“鸡蛋”而不是“填满”,寻求帮助@chridam@user1099855我已经用解决方案更新了我的答案。完美,工作正常