从Mongoose中的数组中删除id

时间:2016-04-17 09:58:35

标签: node.js mongodb mongoose

我遵循了Mongoose Schema

 var UserSchema = mongoose.Schema({
    createdOn: {type: Date, default:Date.now },
    purchaseDate:Date,
    expiryDate:Date,
    isEnabled: {type: Boolean,default: true},
    messages:[{
        message: String,
        location: String,
        date: Date,
    }]
});

当我将文档转换为JSON时,我想删除messages数组中每条消息的_id。我目前的toJSON方法在

之下
UserScheme.methods.toJSON = function() {
    var obj = this.toObject();
    delete obj._id;
    delete obj.__v;
    return obj;
};

0 个答案:

没有答案