沙发数据库;如何直接在 json 的第一级获取文档,而不是在值内分组 - viewWithList

couchdb ; how get documents directly in first level of json, and not grouped inside value - viewWithList

我有一个设计文档:'accounts',以及视图:'accounts-view'

视图的内容是:

function (doc) {
  emit( doc._id, doc);
}

我的快递代码是:

db.view('accounts', 'accounts-view', function(err, body) {
    if (err) throw error;
    res.json(body.rows);
});

结果是:

[
  {
    "id": "8767d3474a0e80dd0ab7d0b0580065af",
    "key": "8767d3474a0e80dd0ab7d0b0580065af",
    "value": {
      "_id": "8767d3474a0e80dd0ab7d0b0580065af",
      "_rev": "1-37eb3e76e4715e9a4fc8930470cc4ca3",
      "type": "accounts",
      "lastname": "Kitchen",
      "firstname": "Peter"
    }
  },
  {
    "id": "8767d3474a0e80dd0ab7d0b058006e3c",
    "key": "8767d3474a0e80dd0ab7d0b058006e3c",
    "value": {
      "_id": "8767d3474a0e80dd0ab7d0b058006e3c",
      "_rev": "1-bcab94bb253c83b4951a787c253896f5",
      "type": "accounts",
      "lastname": "Kolner",
      "firstname": "John"
    }
  }
]

我怎样才能得到这样的东西:(只打印每一行的全部内部值)

[
  {
     "_id": "8767d3474a0e80dd0ab7d0b0580065af",
      "_rev": "1-37eb3e76e4715e9a4fc8930470cc4ca3",
      "type": "accounts",
      "lastname": "Kitchen",
      "firstname": "Peter"

  },
  {

      "_id": "8767d3474a0e80dd0ab7d0b058006e3c",
      "_rev": "1-bcab94bb253c83b4951a787c253896f5",
      "type": "accounts",
      "lastname": "Kolner",
      "firstname": "John"

  }
]

更新:

我已经听从了 Domonique 的建议;现在我有一个新视图,它只发出 id(所以我可以将 space 保存在磁盘上并使用视图上的参数 "include_docs=true" 检索文档):

function(doc) {
  if (doc.type && doc.type=='accounts') {
    emit( doc._id);
  }
}

和一个新列表:

function(head, req) {
  provides('json', function() {
    var results = [];
    while (row = getRow()) {
      //results.push(row.value);
      results.push(row.doc);
    }
    send(JSON.stringify(results));
  });
}

最后我得到了记录:

http://127.0.0.1:5984/crm/_design/crmapp/_list/accounts-list/accounts-view?include_docs=true

结果是:

[
  {
    "_id": "8767d3474a0e80dd0ab7d0b0580065af",
    "_rev": "1-37eb3e76e4715e9a4fc8930470cc4ca3",
    "type": "accounts",
    "lastname": "Kitchen",
    "firstname": "Peter"
  },
  {
    "_id": "8767d3474a0e80dd0ab7d0b058006e3c",
    "_rev": "1-bcab94bb253c83b4951a787c253896f5",
    "type": "accounts",
    "lastname": "Kolner",
    "firstname": "John"
  },
  {
    "_id": "8767d3474a0e80dd0ab7d0b058008e9a",
    "_rev": "1-86078f00be82b97499a0f52488cefbbf",
    "lastname": "Tower",
    "firstname": "George",
    "type": "accounts"
  }
]

我的应用程序节点快递已更新:

db.viewWithList('crmapp', 'accounts-view','accounts-list', {"include_docs":"true"} , function(err, body) {
    if (err) throw err;
    res.json(body);
});

有了这个清单,我就不需要在 express 项目上再减少它了,可以吗? 如何更新我的列表或视图以通过 id 获取?只是在 url 上添加 id 是行不通的;像这样:

http://127.0.0.1:5984/crm/_design/crmapp/_list/accounts-list/accounts-view?include_docs=true&_id=8767d3474a0e80dd0ab7d0b058006e3c

我通过 id 获取所有记录而不是唯一的记录

要在这里回答你的问题,你应该简单地 map 数组并且只包括 value 部分:

db.view('accounts', 'accounts-view', function(err, body) {
  if (err) throw error;
  res.json(body.rows.map(function (row) {
    return row.value;
  }));
});

由于您显然是 CouchDB 的新手,我也会给您一些关于视图的建议。首先,您创建的视图实际上只是系统视图 _all_docs 的副本,因此您应该直接使用它而不是创建您自己的视图。 (特别是因为您已经有效地在磁盘上创建了一个副本)

但是,很可能随着您在应用程序中的进一步发展,您将使用根据查询以不同方式对文档进行分区的真实视图。因此,您应该 而不是 在视图函数中发出整个文档(即:doc)。通过这样做,您可以有效地在磁盘上复制该文档,因为它将在您的数据库以及视图索引中表示。

建议的起点是简单地省略 emit 的第二个参数。

function (doc) {
  emit(doc._id);
}

查询视图时,只需将 include_docs=true 添加到 URL,您的视图将如下所示:

[
  {
    "id": "8767d3474a0e80dd0ab7d0b0580065af",
    "key": "8767d3474a0e80dd0ab7d0b0580065af",
    "value": null,
    "doc": {
      "_id": "8767d3474a0e80dd0ab7d0b0580065af",
      "_rev": "1-37eb3e76e4715e9a4fc8930470cc4ca3",
      "type": "accounts",
      "lastname": "Kitchen",
      "firstname": "Peter"
    }
  }
  // ...
]

然后,您可以检索 doc 而不是 value 以更有效地获得相同的结果。