【问题标题】:How can i delete a product at a specific time in MongoDB如何在 MongoDB 中的特定时间删除产品
【发布时间】:2022-08-18 19:09:58
【问题描述】:

我正在尝试在特定时间删除产品我该怎么做,

expired_at 不起作用

这是我的数据库

const mongoose = require(\'mongoose\');
const Schema = mongoose.Schema;
const ProductSchema = new Schema(
  {
    category: {
      type: Schema.Types.ObjectId,
      ref: \'Category\',
    },
    owner: {
      type: Schema.Types.ObjectId,
      ref: \'Owner\',
    },
    title: String,
    description: String,
    photo: String,
    price: Number,
    stockQuantity: Number,
    reviews: [{ type: Schema.Types.ObjectId, ref: \'Review\' }],
    // expireAt: { type: Date, default: Date.now, expires: \"5m\" }
  },
  {
    toobject: { virtuals: true },
    toJSON: { virtuals: true },
  }
);
const Product = mongoose.model(\'Product\', ProductSchema);

module.exports = Product;

请问我该怎么做

标签: node.js mongodb mongoose


【解决方案1】:

试试这些

 createdAt: {
      type: Date,
      default: Date.now,
      expires: 30
    }

保存后创建一个新产品,以便您获得产品的时间戳,这将在 30 秒内删除该产品

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 2017-01-15
    • 2022-06-22
    • 2019-01-19
    • 1970-01-01
    • 2019-10-23
    相关资源
    最近更新 更多