我有这样的大对象:

const example = {
    startMap: 'Something',
    monsters: [],
    monstersToOmit: [],
    mapsOrder: [1, 2],
    mapsData: [
        {
            id: 1,
            name: 'lol',
            gates: [
                {
                    toId: 2,
                    coords: {
                        x: 49,
                        y: 28
                    }
                }
            ],
            waypoints: [
                [
                    {x: 81, y: 50},
                    {x: 53, y: 59},
                    {x: 64, y: 15},
                    {x: 87, y: 20}
                ],
                [
                    {x: 93, y: 54},
                    {x: 90, y: 10},
                    {x: 67, y: 16},
                    {x: 51, y: 54}
                ],
                [
                    {x: 86, y: 57},
                    {x: 77, y: 19},
                    {x: 59, y: 20},
                    {x: 54, y: 58}
                ]
            ]
        },
        {
            id: 2,
            name: 'nothin',
            gates: [
                {
                    toId: 1,
                    coords: {
                        x: 95,
                        y: 49
                    }
                }
            ],
            waypoints: [
                {x: 40, y: 1},
                {x: 57, y: 8},
                {x: 79, y: 7},
                {x: 81, y: 31},
                {x: 61, y: 28},
                {x: 22, y: 16},
                {x: 11, y: 13},
                {x: 42, y: 49},
                {x: 49, y: 51},
                {x: 78, y: 50},
                {x: 42, y: 37},
                {x: 15, y: 37},
                {x: 7, y: 51}
            ]
        }
    ]
};


我想从中创建猫鼬模式,对于startMap,monsters,monstersToOmit,mapsOrder很容易,但是我不知道如何构造mapsData,因此我可以将example.mapsData.id类型指定为Number和example.mapsData .gates.coords.x也应为Number,依此类推。

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


const mapSchema = new Schema({
    startMap: {
        type: String,
        required: true,
        unique: true
    },
    monsters: {
        type: Array,
        required: true
    },
    monstersToOmit: {
        type: Array,
        required: true
    },
    mapsOrder: {
        type: Array,
        required: true
    },
    mapsData: {
        ???
    }
});

最佳答案

它看起来像这样:

mapsData: [{
    id: Number,
    name: String,
    gates: [{
        toId: Number,
        coords: {
            x: Number,
            y: Number
        }
    }],
    waypoints: [[{x: Number, y: Number}]]
}]


不确定并感觉正常时,您总是可以执行mapsData:JSON

09-20 17:20