【问题标题】:How to CRUD data in mongoose without data modeling如何在没有数据建模的情况下在猫鼬中 CRUD 数据
【发布时间】:2020-04-24 07:43:12
【问题描述】:

我在网上搜索了 2 天,没有找到任何答案,

const mongoose = require('mongoose');

const channelsSchema = new mongoose.Schema(
    {
        stateName: {
            type: String,
            required: [true, 'A state must have a name'],
            unique: true,
            index:true
        },
        id: {
            type:String
        },
        Name: {
            type:String
        },
        **anyThing: [Object]
    },
    { strict: false }
);

const Channels = mongoose.model('Channels', channelsSchema);

module.exports = Channels;

有没有办法**anything 接受任何东西?

注意:这不是一个大项目,我也不担心安全问题。它只在本地运行

【问题讨论】:

    标签: node.js mongodb mongoose crud


    【解决方案1】:

    您可以将 {strict: false} 作为第二个参数传递给 Schema 来实现这一点。

    const mongoose = require('mongoose');
    
    const channelsSchema = new mongoose.Schema({}, {strict: false})
    const Channels = mongoose.model('Channels', channelsSchema);
    module.exports = Channels;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 2018-05-21
      • 1970-01-01
      • 2016-05-28
      • 2016-07-22
      相关资源
      最近更新 更多