Categories
JavaScript Answers

How to output ‘id’ instead of ‘_id’ with Node.js MongoDB?

Spread the love

To output ‘id’ instead of ‘_id’ with Node.js MongoDB, we call set to modify the toJSON method.

For instance, we write

Schema.set("toJSON", {
  virtuals: true,
});

to call set with an object that sets virtuals to true to change _id to id and return it.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *