简体   繁体   English

如何使用Node.js在Redis缓存中创建LIKE运算符搜索?

[英]How to create LIKE operator search in Redis cache using nodejs?

I have a question, is it possible to create a LIKE operator search in Redis? 我有一个问题,可以在Redis中创建LIKE运算符搜索吗? Similar to relational (mysql/oracle) database. 类似于关系(mysql / oracle)数据库。

I have complex json: 我有复杂的json:

   {"_id" : ObjectId("581c8b8854fdcd1ff8c944e0"),
"Objectcode" : "xxxxx",
"Objecttype" : "xxxx",
"docid" : "581c8b8554fdcd1ff8c93d10",
"description" : "Tags based search .... ",
"metaTags" : [ 
    "tag1", 
    "tag2", 
    "tag3", 
    "tag5", 
    "tag6", 
    "tag7", 
    "tag8", 
    "tag9", 
    "tag10"
],
"__v" : 0

} }

and i want to search on array of metaTags how can i do it? 我想搜索metaTags数组怎么办?

Thanks 谢谢

You can use Redis *SCAN commands http://redis.io/commands/scan , depending of your type of data to filter by a pattern: 您可以使用Redis * SCAN命令http://redis.io/commands/scan ,具体取决于要通过模式过滤的数据类型

  • SCAN iterates the set of keys in the currently selected Redis database. SCAN迭代当前选择的Redis数据库中的密钥集。
  • SSCAN iterates elements of Sets types. SSCAN迭代Sets类型的元素。
  • HSCAN iterates fields of Hash types and their associated values. HSCAN迭代Hash类型的字段及其关联的值。
  • ZSCAN iterates elements of Sorted Set types and their associated scores. ZSCAN迭代Sorted Set类型的元素及其关联的分数。

Never use KEYS in app code, because it may ruin performance. 切勿在应用程序代码中使用KEYS ,因为它可能会破坏性能。

The two major nodejs redis client libraries node_redis and ioredis support it, with some syntax sugar: 两个主要的nodejs redis客户端库node_redisioredis支持它,并带有一些语法糖:

const keys = [];
const redis = new Redis(); // ioredis
redis.mset('foo1', 1, 'foo2', 1, 'foo3', 1, 'foo4', 1, 'foo10', 1, () => {
  const stream = redis.scanStream();
  stream.on('data', data => {
    keys = keys.concat(data);
  });
  stream.on('end', () => {
    assert.equal(keys.sort(), ['foo1', 'foo10', 'foo2', 'foo3', 'foo4']);
  });
});

You can use MATCH commands to search data. 您可以使用MATCH命令搜索数据。

Eg: scan 0 MATCH *11*

Refer: http://redis.io/commands/scan 请参阅: http : //redis.io/commands/scan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM