T
T
teertsneerg2020-08-26 12:52:17
Node.js
teertsneerg, 2020-08-26 12:52:17

How to prevent creating _id in SubDocument?

There is the following schema:

const {Schema, model, Types} = require('mongoose');

const schema = new Schema({
        label: {type: String, required: true},
        data: [{
            x: {type: Number},
            y: {type: String}
        }],
        color: {type: String, required: true},
        instrument: {type: Types.ObjectId, ref: 'Instruments', required: true}
    },
    {
        versionKey: false
    }
);

module.exports = model('Strategy', schema, 'Strategies');


Question:
1) When trying to get the data property, it contains { _id, x, y }, how to forbid creating _id or getting a property without _id?

2) instrument returns only a string containing _id. How to get the object that the given _id refers to?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question