猫鼬模式的对象数组 [英] Mongoose schema array of objects

查看:75
本文介绍了猫鼬模式的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的大物件:

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,依此类推.

I want to create mongoose schema from this, it's easy for startMap, monsters, monstersToOmit, mapsOrder but I don't know how to structure mapsData so I'll be able to specify example.mapsData.id type to be Number and example.mapsData.gates.coords.x to be Number as well and so on.

'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.

you can always do mapsData:JSON when you're unsure and feel things out.

这篇关于猫鼬模式的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆