【问题标题】:CouchDB count all documents and return a number?CouchDB 计算所有文档并返回一个数字?
【发布时间】:2021-10-01 21:20:44
【问题描述】:

我们在 CouchDB 中有一个大学项目,我正在使用节点,我想创建一个视图,通过电子邮件返回我的所有文档。

我找不到任何有用的东西,我不确定我错过了什么,我尝试了很多不同的 reduce 函数和 emit 方法。

感谢您的任何回答。

文档有 2 个字段,名称和电子邮件

【问题讨论】:

  • 请避免张贴图片,因为这样做是对视力障碍者的排斥。

标签: node.js database express couchdb


【解决方案1】:

不要使用db endpoint,因为响应字段doc_count 包括设计文档以及可能没有email 字段的其他文档。

执行此操作的直接方法是使用视图。代码 sn-p 演示了 db info doc_count 和使用 PouchDB 的视图的 total_rows 之间的区别。我猜这个索引可能还有更有趣的用途。

设计文档很简单

{
 _id: '_design/my_index',
   views: {
     email: {
       map: function(doc) {
         if (doc.email) emit(doc.email);
       }.toString()
    }
  }
}

而且视图查询非常高效简单。

db.query('my_index/email', {
        include_docs: false,      
        limit: 0
      })

const gel = id => document.getElementById(id);
let db;

function setJsonToText(elId, json) { 
  gel(elId).innerText = JSON.stringify(json, undefined, 3);
}
async function view() {
  // display db info
  setJsonToText('info', await db.info());
  // display total number or rows in the email index
  const result = await db.query('my_index/email', {
    include_docs: false,  
    limit: 0
  });
  setJsonToText('view', result);

}

// canned test documents
function getDocsToInstall() {
  return [{
      email: 'jerry@garcia.com',
    },
    {
      email: 'bob@weir.com',
    },
    {
      email: 'phil@lesh.com'
    },
    {
      email: 'wavy@gravy.com'
    },
    {
      email: 'samson@delilah.com'
    },
    {
      email: 'cosmic@charlie.com'
    },
    // design doc
    {
      _id: '_design/my_index',
      views: {
        email: {
          map: function(doc) {
            if (doc.email) emit(doc.email);
          }.toString()
        }
      }
    }
  ]
}

// init example db instance
async function initDb() {
  db = new PouchDB('test', {
    adapter: 'memory'
  });

  await db.bulkDocs(getDocsToInstall());

};

(async() => {
  await initDb();
  await view();
})();
<script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb-7.1.1.min.js"></script>
<script src="https://github.com/pouchdb/pouchdb/releases/download/7.1.1/pouchdb.memory.min.js"></script>
<pre>Info</pre>
<pre id='info'></pre>
<div style='margin-top:2em'></div>
<pre>email view</pre>
<pre id='view'>
</pre>

【讨论】:

  • 非常感谢您,我稍后会检查它!
【解决方案2】:

您可以使用GET /{db},它返回有关指定数据库的信息。这是一个包含属性doc_countJSON 对象。

doc_count (number) – 指定数据库中的文档计数。

以 Angular 为例,这可以通过以下方法完成:

async countDocuments(database: string): Promise<number> {
  return this.http.get<any>(this.url('GET', database), this.httpOptions).toPromise()
    .then(info => info['doc_count']);
}

【讨论】:

  • 注意 db 端点返回所有个文档的计数,包括设计文档
【解决方案3】:

假设:

假设客户数据库中存在以下文档:

[
{
  "_id": "93512c6c8585ab360dc7f535ff00bdfa",
  "_rev": "1-299289ee89275a8618cd9470733035f4",
  "name": "Tom",
  "email": "tom@domain.com"
 },
{
  "_id": "93512c6c8585ab360dc7f535ff00c930",
  "_rev": "1-a676883d6f1b5bce3b0a9ece92da6964",
  "name": "Tom Doe",
  "email": "tom@domain.com"
 },
{
  "_id": "93512c6c8585ab360dc7f535ff00edc0",
  "_rev": "1-09b5bf64cfe66af7e1134448e1a328c3",
  "name": "John",
  "email": "john@domain.com"
 },
{
  "_id": "93512c6c8585ab360dc7f535ff010988",
  "_rev": "1-88e347af11cfd1e40e63920fa5806fd2",
  "name": "Alan",
  "email": "alan@domain.com"
 }


]

如果我正确理解您的查询,那么根据以上数据,您需要以下给定的结果集。

{
"tom@domain.com": 2,
"alan@domain.com": 1,
"john@domain.com": 1
}

解决方案:

为了实现上述目的,请考虑以下包含具有 Map 和 Reduce 功能的 View 的设计文档。

{
  "_id": "_design/Customers",
  "views": {
    "by-email": {
      
      "map": "function (doc) {
         if(doc.email){
              emit(doc.email, doc._id);
         }
       }",
      "reduce": "_count"
    }
  },
  "language": "javascript"
}

如果文档中存在键,则上述视图函数发出文档的键email 的值。

reduce 函数 _count 是一个内置的 reducer(由 CouchDB 提供),用于执行计数逻辑。

执行视图查询:

为了查询这个view,你需要:选择视图函数,标记reduce to be executed(因为它是可选的运行reduce)并将1设置为组级别。

您可以通过 UI 执行此操作:

结果:

这是上述查询给出的结果: [![map reduce查询结果

希望这会有所帮助。 有关其他 reduce 函数和组级别的更多详细信息,请参阅 CouchDB 文档。 干杯。

【讨论】:

    猜你喜欢
    • 2018-08-26
    • 2012-07-02
    • 1970-01-01
    • 2017-10-19
    • 2020-10-25
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多