寻找索引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_index 和 https://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 以将 invoiceNumber
和 barCode
作为索引,如下所示:
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/