Node_redis - 如何删除一个键?

60
有没有办法使用Node_redis按键删除一个条目? 我从文档中找不到这样的选项。
7个回答

137
你可以这样使用 del:
redis.del('SampleKey');

8
这是实际的答案,提供 Redis 文档链接并不是一个答案,因为问题标记了 node-redis - Sandwich

53

在这里,您可以看到此库中使用的redis命令node_redis github

如您所见,“del”命令在列表中。

而且,正如Jonatan所回答的那样,此命令允许您从所选数据库中删除键。


@Elephant hdel不在,但它可以工作。参考网址应为https://redis.io/commands,因为“它支持所有Redis命令”(引自github https://github.com/NodeRedis/node_redis)。 - TigOldBitties
@Elephant 不用在意,我正在看 ioredis https://github.com/luin/ioredis/blob/master/lib/command.js - TigOldBitties
2
提供的链接现在在github.com上已经404了。 - Jacob Degeling

23

正如其他人所说,你可以使用del函数。你可以通过以下语法确保成功删除操作。

client.del('dummyvalue', function(err, response) {
   if (response == 1) {
      console.log("Deleted Successfully!")
   } else{
    console.log("Cannot delete")
   }
})

因为DEL命令在操作成功时会返回(integer) 1

    redis 127.0.0.1:6379> DEL key 
    Success: (integer) 1
    Unsuccess: (integer) 0

13

如果我记得正确的话,del应该可以做到。


9

希望这可以帮助到您

let redis = require("redis");

var redisclient = redis.createClient({
  host: "localhost",
  port: 6379
});

redisclient.on("connect", function () {
  console.log("Redis Connected");
});

redisclient.on('ready', function () {
  console.log("Redis Ready");
});

redisclient.set("framework", "AngularJS", function (err, reply) {
  console.log("Redis Set" , reply);
});

redisclient.get("framework", function (err, reply) {
  console.log("Redis Get", reply);
});

redisclient.del("framework",function (err, reply) {
  console.log("Redis Del", reply);
});

0
假设我们想要删除 key="randomkey":
redisClient.del(key, function(err, reply) {
  if (err)
    throw err;

  console.log("deleteRedisKey",reply)
  resolve(reply);
});

-10
使用sailsJs,您可以这样做:
let id_todel = "5b845f9ea7f954bb732fdc52";
await sails.getDatastore('redis').leaseConnection(function during(db, proceed) {
      db.del(id_todel );
      return proceed(undefined, undefined);
});

这是特定于sails的。 - Kaushik Thirthappa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接