Johnny Johnny - 2 years ago 87
Node.js Question

MongoDB: output 'id' instead of '_id'

I am using mongoose (node), what is the best way to output id instead of _id?

Answer Source

I create a toClient() 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:

Schema.method('toClient', function() {
    var obj = this.toObject();

    //Rename fields = obj._id;
    delete obj._id;

    return obj;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download