Why mongodb output _id instead of id?
Given you're using Mongoose, you can use 'virtuals', which are essentially fake fields that Mongoose creates. They're not stored in the DB, they just get populated at run time:Any time is called on the Model you create from this Schema, it will include an field that matches the field Mongo generates. Likewise, you can set the behavior for in the same way.See:http://mongoosejs.com/docs/api.htmlhttp://mongoosejs.com/docs/guide.html#toJSONhttp://mongoosejs.com/docs/guide.html#toObjectYou can abstract this into a BaseSchema for all your models, then extend/invoke to keep the logic in one place. I wrote the above while creating an Ember/Node/Mongoose app, since Ember really prefers to have an field to work with.As of Mongoose v4.0, part of this functionality is supported out of the box. It's no longer required to manually add a virtual field as explained by @Pascal Zajac. Mongoose assigns each of your schemas an virtual getter by default, which returns the document's field cast to a string, or in the case of ObjectIds, its hexString. If you don't want an getter added to your schema, you may disable it by passing this option at schema construction time. Source However, to export this field to JSON, it's still required to enable serialization of virtual fields: I used this: I think it would be great if they automatically suppress when is true. I create a method on my models where I do this. It's also a good place to rename/remove other attributes you don't want to send to the client: Here is an alternative version of the answer provided by @user3087827. If you find that is undefined, then you can use: MongoDB uses as the default field to store the unique identifier of documents (primary key). There are several reasons behind this decision: Uniqueness: MongoDB designs the field to ensure that each document in a collection can be uniquely identified. MongoDB automatically generates an ObjectId type value for each new document, which is globally unique—meaning that two documents will not have the same even across different servers or clusters. Consistency: Using as the standard field name for all documents provides a consistent interface for developers to reference the primary key. This consistency simplifies data model design and allows developers to write more generic code for handling different documents. Indexing: In MongoDB, each collection automatically creates a unique index on the field. This design ensures fast lookup and efficient data integrity checks. Without this default index, developers would need to manually create indexes for their chosen primary key fields, increasing development complexity. For example, consider a user collection (collection) containing many user documents (document). Each document will automatically have an field, as follows: Even if developers choose to use a custom field (such as ) as the document identifier, it is generally recommended to use the default field to maintain consistency and leverage MongoDB's built-in indexing. In cases where specific needs arise, developers can map to or any other field they prefer at the application layer. In such cases, developers are responsible for maintaining the uniqueness and indexing of the additional fields.