索引 Pouch db 多维文档

Indexing Pouch db multi dimensional documents

寻找索引 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...


现在我们要在地图函数中多次使用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