Changing Mongoose _id to id

I would like to know how I can change _id to id practically or anyway, so direct json output from the database looks pretty. Also, I see __v in my docs and am not sure how to hide these fields.

+8
source share
2 answers

if you want to hide __v in the mongodb collection, use versionKey: false in the collection schema definition.

Example:

'use strict';

const mongoose = require('mongoose');

export class DeviceID extends mongoose.Schema {

    constructor() {
        super({
            device_id: String
        },
        {
            versionKey: false
        });
    }

}
+2
source

I suggest the following:

MongoDB: prints 'id' instead of '_id'

to abort the JSON that you send to the client by creating a schema method

and this is to uninstall the Key version:

Hide Mongoose __v property?

+1

Source: https://habr.com/ru/post/1676878/


All Articles