node.js 将元数据附加到 json

node.js append meta data to json

我有一个 node.js api,它使用猫鼬从 mongodb 中提取数据,并想将一些元数据附加到返回的 json 文档中。这样做的最佳方法是什么?

返回文档的当前格式

[
    {
    _id: "55edf1cc49f2dd46365b0884",
    title: "title 1",
    body: "body 1"
    },
    {
    _id: "55edfffe49f2dd46365b0885",
    title: "title 2",
    body: "body 2"
    }
]

返回文档的要求格式

{
    "data": [
        {
        _id: "55edf1cc49f2dd46365b0884",
        title: "title 1",
        body: "body 1"
        },
        {
        _id: "55edfffe49f2dd46365b0885",
        title: "title 2",
        body: "body 2"
        }
    ],
    "meta": {
        "test": "test"
    }
}

一个明显的解决方案是(假设您使用的是 Express):

res.json({
  data : mongooseResult,
  meta : { test : 'test' }
});