node.js Node_redis - 如何删除密钥?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15219577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:45:15  来源:igfitidea点击:

Node_redis - how to remove a key?

node.jsredisnode-redis

提问by UpTheCreek

Is there any way to remove/delete an entry by key, using Node_redis? I can't see any such option from the docs..

有没有办法使用Node_redis按键删除/删除条目?我从文档中看不到任何这样的选项..

回答by himanshu yadav

You can deluse like this:

你可以这样del使用:

redis.del('SampleKey');

回答by Elephant

Here you can see what redis commands are work in this library node_redis github

在这里你可以看到在这个库node_redis github 中有哪些 redis 命令起作用

As you can see "del" command is in the list.

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

And this command allow you delete keys from selected db as Jonatan answered.

并且此命令允许您从选定的数据库中删除键,就像乔纳坦回答的那样。

回答by Tolsee

As everyone above has stated you can use del function. You can assure the successful delete operation using this syntax.

如上所述,您可以使用 del 函数。您可以使用此语法确保删除操作成功。

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

Because the DEL command will return (integer) 1in successful operation.

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

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

回答by Jonatan Hedborg

If I remember things correctly, delshould do it.

如果我没记错的话,del应该这样做。

回答by Renish Gotecha

Hope this will help you

希望能帮到你

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);
});

回答by Zakaria.dem

Using sailsJs you can do this :

使用sailsJs你可以做到这一点:

let id_todel = "5b845f9ea7f954bb732fdc52";
await sails.getDatastore('redis').leaseConnection(function during(db, proceed) {
      db.del(id_todel );
      return proceed(undefined, undefined);
});