mongoose在toJSON.transform上忽略具有相同键的嵌套项属性

时间:2016-06-08 13:56:51

标签: node.js mongodb mongoose mongoose-schema

我是nodejs,mongoose等的新手。我正在尝试在json转换过程中转换我的模型。

目前,我正在使用下面的转换功能;

Schema.options.toJSON.transform = function (doc, ret, options) {
  ret.customerId = ret._id;
  delete ret._id;
}

对于这样的架构:

{
    _id: "customer_id",
    name: "customer_name",
    items: [{
        _id: "item_id",
        name: "item_name"
    }]
}

问题是,即使对于“items”数组中的元素,我的transform函数也会替换所有“_id”定义。

就像;

{
    customerId: "customer_id",
    name: "customer_name",
    items: [{
        customerId: "item_id",
        name: "item_name"
    }]
}

我在google上进行了一些搜索,但找不到任何解决方案。

有没有办法做到这一点?如果有人帮助我,我会很高兴...

感谢。

0 个答案:

没有答案