javascript - 索引 Pouch db 多维文档

标签 javascript pouchdb

寻找索引pouchDB的方法

当我有多个维度时找不到索引的方法

这是我的文档客户端示例

注意客户可能有几张发票

{
    clientId : 2
    clientName : 'toto'
    phoneNumber : '2342342'
    invoices : [
        {
            invoiceNumber : '12312313' , 
            Amount : 234242, 
            barCode : '1234324', 
        },  
        {
            invoiceNumber : '12312313' , 
            Amount : 234242, 
            barCode : '1234324', 
        }
    ]
}
{
    clientId : 3
    clientName : 'tata'
    phoneNumber : '2342342'
    invoices : [
        {
            invoiceNumber : '3542435' , 
            Amount : 234242, 
            barCode : '1234324', 
        },  
        {
            invoiceNumber : '235423' , 
            Amount : 234242, 
            barCode : '23454235', 
        }
    ]
}

我希望能够通过发票号和条码号找到客户

所以索引这些很重要

谢谢你的帮助

我看过https://pouchdb.com/api.html#create_indexhttps://pouchdb.com/2014/05/01/secondary-indexes-have-landed-in-pouchdb.html

目前运气不佳

最佳答案

CouchDB documentation 所述:

... the emit() function can be called multiple times in the map function to create multiple entries in the view results from a single document...


现在我们将在 map 函数中多次使用 emit()。此外,我们将发出 arrays 以将 invoiceNumberbarCode 作为索引,如下所示:

var myIndex = {
    _id: '_design/my_index',
    views: {
        'my_index': {
            map: function (doc) {
                for(var i=0, length=doc.invoices.length; i<length; i++){
                    emit(
                        // emit array of invoiceNumber and barCode as key:
                        [doc.invoices[i].invoiceNumber, doc.invoices[i].barCode],
                        // emit array of clientId and clientName as value
                        // Actually, value can be whatever you want:
                        [doc.clientId, doc.clientName]
                    );
                }
            }.toString()
        }
    }
};

现在PUT我们上面的设计文档并用PouchDB查询它:

pouch.put(myIndex).then(() => {
  // query the index
  return pouch.query('my_index', {key: ['12312313','1234324']});
}).then(result => {
  // found docs with invoiceNumber === '12312313' and barCode === '1234324'
  console.log('Result: ', result)
});

另请查看 this similar answer .

关于javascript - 索引 Pouch db 多维文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49575033/

相关文章:

javascript - 在与 Fabric.js 关联的 Canvas 之外使用 Cufon

JavaScript "this"在事件处理程序中引用了错误的对象

javascript - JQuery 选择器的误解

javascript - 无论对象数组中的深度如何,获取键值

javascript - 如何在 Angular 中启用 PouchDB 调试

javascript - 是否可以在 .trigger() 函数上使用 .stopPropagation() ?

javascript - 如何加密 pouchdb 数据库

javascript - PouchDB 中模糊的用户名和密码

couchdb - 'continuous'对于PouchDB的replicate()意味着什么?

javascript - 如何删除 pouchDB 中的身份验证模式