【问题标题】:Indexing Pouch db multi dimensional documentsIndexing Pouch db 多维文档
【发布时间】:2018-03-30 13:06:32
【问题描述】:

寻找一种方法来索引 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

到目前为止运气并不好

【问题讨论】:

    标签: javascript pouchdb


    【解决方案1】:

    CouchDB documentation 所述:

    ...emit() 函数可以在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-06
      • 2017-05-30
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      相关资源
      最近更新 更多