You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js调用Cloudant DB的.list()时如何去除doc中的_id和_rev

问题

我正在使用Node.js对Cloudant DB执行.list()调用,操作的数据库为history_metric。当前调用代码如下:

CloudantConnection.use("history_metric")
    .list({
      include_docs: true,
      limit: 12,
    })
    .then((res) => {
      response = { status: true, res };
    })
    .catch((error) => {
      response = { status: false, error };
    });

当前返回结果如下:

{
    "success": true,
    "data": {
        "status": true,
        "res": {
            "total_rows": 1,
            "offset": 0,
            "rows": [
                {
                    "id": "012022",
                    "key": "012022",
                    "value": {
                        "rev": "1-0d48e71b8a774b394c2b6f380e600cde"
                    },
                    "doc": {
                        "_id": "012022",
                        "_rev": "1-0d48e71b8a774b394c2b6f380e600cde",
                        "year": 2022,
                        "month": "01",
                        "count": 0
                    }
                }
            ]
        }
    }
}

我希望返回结果中的doc字段不包含_id和_rev,仅保留指定字段,期望的返回格式如下:

{
    "success": true,
    "data": {
        "status": true,
        "res": {
            "total_rows": 1,
            "offset": 0,
            "rows": [
                {
                    "id": "012022",
                    "key": "012022",
                    "value": {
                        "rev": "1-0d48e71b8a774b394c2b6f380e600cde"
                    },
                    "doc": {
                        "year": 2022,
                        "month": "01",
                        "count": 0
                    }
                }
            ]
        }
    }
}

请问是否有方法实现这一需求?

解决方案

方法一:客户端过滤返回结果

直接在.then()回调中处理返回数据,移除doc里的_id和_rev字段,代码修改如下:

CloudantConnection.use("history_metric")
    .list({
      include_docs: true,
      limit: 12,
    })
    .then((res) => {
      // 遍历每一行数据,清理doc字段
      const processedRows = res.rows.map(row => {
        // 解构排除_id和_rev,保留其他字段
        const { _id, _rev, ...cleanDoc } = row.doc;
        return { ...row, doc: cleanDoc };
      });
      // 替换处理后的rows,保持其他结构不变
      response = { status: true, res: { ...res, rows: processedRows } };
    })
    .catch((error) => {
      response = { status: false, error };
    });

这种方式简单快捷,适合数据量不大的场景,无需修改Cloudant端配置。

方法二:Cloudant视图实现服务器端过滤

如果希望从Cloudant直接返回过滤后的字段,可以创建自定义视图,在服务器端完成字段筛选:

  1. 在Cloudant数据库中创建设计文档(例如_design/history_metric_view),添加map函数:
function(doc) {
  // 仅返回需要的字段
  emit(doc._id, { year: doc.year, month: doc.month, count: doc.count });
}
  1. 使用视图查询替代.list()调用:
CloudantConnection.use("history_metric")
    .view("history_metric_view", "your-view-name", {
      limit: 12
    })
    .then((res) => {
      // 调整返回结构匹配需求
      const processedRows = res.rows.map(row => ({
        id: row.id,
        key: row.key,
        value: { rev: row.value._rev }, // 若需要rev可在map函数中额外返回
        doc: row.value
      }));
      response = { status: true, res: { ...res, rows: processedRows } };
    })
    .catch((error) => {
      response = { status: false, error };
    });

这种方式适合大数据量场景,能减少网络传输的数据量,但需要额外配置Cloudant视图。


内容的提问来源于stack exchange,提问作者Leonardo Gusmão

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 13:03:22