【问题标题】:How do I use nested schemas in mongoose, using 'type', to create arrays?如何使用 'type' 在 mongoose 中使用嵌套模式来创建数组?
【发布时间】:2019-07-19 08:04:36
【问题描述】:

我正在尝试创建一个嵌套的猫鼬模式,它使用“类型”来创建一个嵌套数组。

我认为我遇到问题的架构是“chorePerson”。

这是我试图放入架构中的数据:

{
  "chart": [
    {
      "ordinal": 0,
      "chorePerson": [
        {
          "person": "emily",
          "chore": "Catbox"
        },
        {
          "person": "Steve",
          "chore": "Dishes"
        }
      ]
    }
  ]

这是我当前的架构。注意“chart”和“chorePerson”使用“type”

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const chorePersonSchema = new mongoose.Schema({
    person: {type: String, requried: true},
    chore: {type: String, required: true},
});


const chartSchema = new mongoose.Schema({
    ordinal: {type: Number, required: true},
    chorePerson:{ type: chorePersonSchema },
});


// create the schema
const ChoreChartSchema = new Schema({

    affiliation: {type: String, required: true},
    currentWeekNumber: {type: Number, required: true},
    currentYear: {type: Number, required: true},

    chart:{ type: chartSchema },

    date: {type: Date, default: Date.now},
})

module.exports = ChoreChart = mongoose.model('cm_chorechart', ChoreChartSchema)

当我运行我的代码时,这是我在崩溃之前得到的:

{ _id: 5c742ed116a095522c38ddfc,
  affiliation: 'family',
   currentYear: 2019,
   currentWeekNumber: 9,
   date: 2019-02-25T20:26:33.914Z,
  chart: [ { ordinal: 0, chorePerson: [Array] } ] }

我认为... chorePerson 导致错误...但我不知道如何解决它。 这是一个例外:

(node:6728) UnhandledPromiseRejectionWarning: ObjectExpectedError: Tried to set nested object field `chart` to primitive value `[object Object]` and strict mode is set to throw.

我的尝试

我试过这个架构:

const chartSchema = new mongoose.Schema({
    ordinal: {type: Number, required: true},

    chorePerson : [{
        person : String,
        chore : String
        }]       
});

更新:

好的...所以我回到了基础,这很有效,但这不是我想要的最终模式。任何人都可以帮忙嵌套这个吗?

// create the schema
const ChoreChartSchema = new Schema({

    affiliation: {type: String, required: true},
    currentWeekNumber: {type: Number, required: true},
    currentYear: {type: Number, required: true},

//    chart:{ type: chartSchema },

    chart:[{
            ordinal: 0,
            chorePerson : [{
                person : String,
                chore : String
            }]
    }],

    date: {type: Date, default: Date.now},
})

【问题讨论】:

    标签: node.js mongoose nested schema


    【解决方案1】:

    事实证明这比我想象的要容易:

        const mongoose = require('mongoose');
        const Schema = mongoose.Schema;
    
        const chorePersonSchema = new mongoose.Schema({
            person: {type: String, requried: true},
            personID: {type: String, required: true},
            chore: {type: String, required: true},
            choreID: {type: String, required: true},
        });
    
        const chartSchema = new mongoose.Schema({
            ordinal: {type: Number, required: true},
    
            chorePerson : [{ type:chorePersonSchema }]       
        });
    
    
        // create the schema
        const ChoreChartSchema = new Schema({
    
            affiliation: {type: String, required: true},
            weekNumber: {type: Number, required: true},
            year: {type: Number, required: true},
    
            chart:[{type: chartSchema}],
    
            date: {type: Date, default: Date.now},
        })
    
        module.exports = ChoreChart = mongoose.model('cm_chorechart', ChoreChartSchema)
    

    【讨论】:

    • 你怎么写图表:[{type: chartSchema} OR {type: someOtherSchema}],我的意思是图表可以是chartSchema对象或someOtherScheme对象的数组..是否可以做一个联合类型?谢谢
    猜你喜欢
    • 2018-03-30
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 2022-12-19
    • 2015-03-29
    • 1970-01-01
    相关资源
    最近更新 更多