如何删除Node.js中的弃用警告

时间:2020-04-08 13:05:39

标签: node.js mongodb mongoose

我正在使用mongoose-ttl节点程序包来清除集合。但我收到此警告。

(node:4768) DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead.

我没有使用诸如remove()之类的任何方法,但是我不知道从何处抛出该警告。 请建议我任何消除此警告的方法。 TTL代码在这里:

var mongoose = require("mongoose");
var ttl = require('mongoose-ttl');

var OTPStore = new mongoose.Schema({
  OTPNo: {
    type: Number,
    require: true
  }
});
OTPStore.plugin(ttl, { ttl: '30m', interval: '30m'});
module.exports = mongoose.model("OTPStore", OTPStore);

谢谢。

1 个答案:

答案 0 :(得分:1)

您似乎想从数据库中删除某些内容。

const store=await OTPStore.deleteOne({_id:id}) //this will find the first one  and delete

 const store=await OTPStore.deleteMany({ _id:id })
//this will delete all. This used for properties that some of the instances have others not. For example if you had "isExpensive" property in your schema you could pass {isExpensive:true} and mongose would find all the instances that meet this criteria and remove them 

const store=await OTPStore.findByIdAndRemove(id) 
// if you notice in above examples I passed an object. But here you are already notifying mongooose that you are querying by id, so just put an id as argument

请注意,此操作是异步操作,因此为了使用await,请确保包装函数应以async function开头

相关问题