1

I have big object like this:

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}
            ]
        }
    ]
};

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: {
        ???
    }
});

1 Answer 1

5

It would look likes this:

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I'll accept as soon as I check if it works correctly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.