【问题标题】:How to prevent the plugin for nested schemas如何防止嵌套模式的插件
【发布时间】:2019-12-24 14:41:54
【问题描述】:

我有一个类别的猫鼬模式

import mongoose from 'mongoose';
import { imageScheme } from './data.schemas';

mongoose.plugin(require('mongoose-delete'));
mongoose.plugin(require('mongoose-timestamp'));
const Schema = mongoose.Schema;
const catScheme = new Schema({
    title: {
        type: String
    },
    subTitle: {
        type: String
    },
    description: {
        type: String
    },
    images: [imageScheme],
    icon: {
        type: String
    },
    deleteAt: {
        type: Date,
        default: null
    }
});

const Category = mongoose.model('Category',catScheme);

export default Category;

我的图像架构位于另一个文件中

const imageScheme = new Schema({
    imageCaption : {
        type: String
    },
    imageFileName: {
        type: String,
        required: true
    },
    imagePath: {
        type: String,
        required: true
    } 
});

当我使用模型创建新类别时,它会为我的图像数组添加时间戳并删除,这意味着它将插件(mongoose-delete、mongoose-timestamp)加载到子架构中

如何避免这种情况?

这就是结果

{
    "_id" : ObjectId("5d5a822f20179023e008f10c"),
    "deleteAt" : null,
    "deleted" : false,
    "title" : "Eyes and Ears",
    "description" : "here you will find all ppe to keep your eye 6 sharp",
    "icon" : "glass.svg",
    "images" : [ 
        {
            "_id" : ObjectId("5d5a822f20179023e008f10e"),
            "deleted" : false,
            "imageCaption" : "Eyes and Ears",
            "imageFileName" : "67acca17-3fc8-416c-bc00-8273770b2115.jpeg",
            "imagePath" : "resources/images/67acca17-3fc8-416c-bc00-8273770b2115.jpeg",
            "updatedAt" : ISODate("2019-08-19T11:04:15.756Z"),
            "createdAt" : ISODate("2019-08-19T11:04:15.756Z")
        }, 
        {
            "_id" : ObjectId("5d5a822f20179023e008f10d"),
            "deleted" : false,
            "imageCaption" : "Eyes and Ears",
            "imageFileName" : "9f1c5b1f-c1be-48f2-a9bc-8294930fd4c9.jpeg",
            "imagePath" : "resources/images/9f1c5b1f-c1be-48f2-a9bc-8294930fd4c9.jpeg",
            "updatedAt" : ISODate("2019-08-19T11:04:15.756Z"),
            "createdAt" : ISODate("2019-08-19T11:04:15.756Z")
        }
    ],
    "updatedAt" : ISODate("2019-08-19T11:04:15.756Z"),
    "createdAt" : ISODate("2019-08-19T11:04:15.756Z"),
    "__v" : 0
}

【问题讨论】:

    标签: javascript node.js mongodb typescript mongoose


    【解决方案1】:

    看起来您正在将mongoose-deletemongoose-timestamp 插入整个数据库。

    您是否尝试过插件页面中提供的示例?

    catScheme.plugin(mongoose-timestamp)

    catScheme.plugin(mongoose-delete)

    【讨论】:

      猜你喜欢
      • 2019-08-23
      • 1970-01-01
      • 2023-02-14
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      相关资源
      最近更新 更多