索引Pouch db多维文档 [英] Indexing Pouch db multi dimensional documents

查看:86
本文介绍了索引Pouch db多维文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找索引pouchDB的方法

Looking for a way to index pouchDB

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

Can't find a way to index when i have multiple dimensions

这是我的文档客户示例

请注意,客户可能有一些发票

Note that client may have a few invoice

{
    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', 
        }
    ]
}

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

I want to be able to find clients by invoice number and barCode number

因此索引这些关键字很重要

So indexing those are important

感谢您的帮助

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

到目前为止运气不大

推荐答案

CouchDB文档中所述


... emit()函数可以在map函数中多次调用 ,以在单个文档的视图结果中创建多个条目...

... 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()。另外,我们将发出数组以同时具有 invoiceNumber barCode 作为索引,像这样:


Now we are going to use emit() multiple times in the map function. Also we are going to emit arrays to have both invoiceNumber and barCode as the index, like this:

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查询它:

Now lets PUT our above design document and query it with 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)
});






也请查看类似的答案

这篇关于索引Pouch db多维文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆