使用猫鼬从对象数组中进行软删除

时间:2020-10-07 06:00:05

标签: node.js mongoose

我正在尝试使用mongoose-delete lib

从数组中软删除对象

我有一个这样的架构:

const mongooseDelete = require('mongoose-delete');

const number = new Schema({
    allocation_date: { type: Date, required: true, default: Date.now() },
    number: { type: String, required: true },
    virtual_number: { type: String, required: true },
    virtual_number_id: { type: Number, required: true, unique: true },
    
});

const schema = new Schema(
    {
        allocation_id: { type: String, required: true, unique: true },
        numbers: { type: [number], required: true },
    },
    {
        timestamps: true,
    },
);

number.plugin(mongooseDelete, { deletedAt : true });

大量数据:

   {
      "_id": "5f7c14f9388ee9ebc0b28abc",
      "allocation_id": "5_2_9387323_1"
      "numbers": [
        {
          "allocation_date": "2020-10-06T06:55:41.197Z",
          "_id": "5f7c14f997d996f7988fe8cf",
          "number": "*********",
          "virtual_number": "**********",
          "virtual_number_id": *******,
        },
        {
          "allocation_date": "2020-10-06T06:59:41.197Z",
          "_id": "5f7c14f997d996f7988fe8cf",
          "number": "*********",
          "virtual_number": "**********",
          "virtual_number_id": *******
        }
      ],
      "createdAt": "2020-10-06T06:55:53.367Z",
      "updatedAt": "2020-10-06T06:55:53.367Z",
    }

当我尝试如下使用delete方法时:

const deleteNumber = await VirtualNumberAllocation.findOne(
        {
            'numbers.virtual_number': virtualNumber.virtualNumber,
            allocation_id: virtualNumber.allocationID,
        });

const activeVirtualNumber = deleteNumber.numbers.filter(
            (number) => number.virtual_number==virtualNumber.virtualNumber,
        );

await activeVirtualNumber[0].delete(); 

我在控制台中收到此错误:

mongoose: calling `save()` on a subdoc does **not** save the document to MongoDB, it only runs save middleware. Use `subdoc.save({ suppressWarning: true })` to hide this warning if you're sure this behavior is right for your app.

有人有使用库从阵列中进行软删除的经验吗?

0 个答案:

没有答案