admin管理员组文章数量:1305408
I would like to get all documents with id matching a regex expression.
For example I have the following doc ids:
p0
p0/e0
p1
p1/e0
How can I get only p0 and p1 ? Regex would be /^p[0-9]+$/
Currently I can do it performing two requests, but I would like to use only one:
This.db.allDocs({
include_docs: false
}).then(function(result){
// Find all ids matching /^p[0-9]+$/
var iDoc = result.rows.length;
while(iDoc--){
if(result.rows[iDoc].id.match(/^p[0-9]+$/)){
projectsIds.push(result.rows[iDoc].id);
}
}
// Get all documents with ids matching /^p[0-9]+$/
This.db.allDocs({
include_docs: true,
keys: projectsIds
}).then(function(result) {
var iProject = result.rows.length;
var docs = [];
while (iProject--) {
docs[iProject] = result.rows[iProject].doc;
}
projects.resolve(docs);
});
});
I would like to get all documents with id matching a regex expression.
For example I have the following doc ids:
p0
p0/e0
p1
p1/e0
How can I get only p0 and p1 ? Regex would be /^p[0-9]+$/
Currently I can do it performing two requests, but I would like to use only one:
This.db.allDocs({
include_docs: false
}).then(function(result){
// Find all ids matching /^p[0-9]+$/
var iDoc = result.rows.length;
while(iDoc--){
if(result.rows[iDoc].id.match(/^p[0-9]+$/)){
projectsIds.push(result.rows[iDoc].id);
}
}
// Get all documents with ids matching /^p[0-9]+$/
This.db.allDocs({
include_docs: true,
keys: projectsIds
}).then(function(result) {
var iProject = result.rows.length;
var docs = [];
while (iProject--) {
docs[iProject] = result.rows[iProject].doc;
}
projects.resolve(docs);
});
});
Share
Improve this question
edited May 11, 2015 at 10:42
sylvain1264
asked May 11, 2015 at 9:21
sylvain1264sylvain1264
8552 gold badges8 silver badges23 bronze badges
1
- what kind of regular expression you use?can you provide more information on that – Bhavesh Jariwala Commented May 11, 2015 at 10:13
3 Answers
Reset to default 6this possible to get document by prefix, for example
localDB.allDocs({
include_docs: true,
startkey: "p0",
endkey: "p0\uffff"
},...);
above code gives you all document which _id is start with p0.
refer link https://github./nolanlawson/pouchdb-quick-search#autosuggestions-and-prefix-search
You need to fetch all your docs using allDocs()
and then filter in memory using JavaScript.
This requires reading your entire database into memory, but PouchDB cannot index on a regex, so it's what you have to do! Else you can design your IDs so that it's easier to do prefix searching as described by the other menter.
A few years later and it looks like pouchdb-find
plugin might have helped (written by @nlawson above, i think). Here's the Pouch docs talking about querying with it... https://pouchdb./api.html#query_index
And how you'd do it:
async test()
let result = await This.db.find({
selector: {_id: {$regex: '^p[0-9]+$'}}
});
let docs = result.rows.map( row => row.doc )
console.dir(docs)
}
(i made it async just so you don't have to faff about with callbacks)
You can try it out for yourself on pouchdb-find plugin's site.
Performance may be terrible though, I've not tried it. @nlawson seems to think a regex on an index can't be done quickly and he surely knows better than most.
EDIT: I just checked the Pouch docs mentioned above and indexes are definitely used during the find process, so performance might be ok.
本文标签: javascriptPouchDb fetch all documents by Id matching regexStack Overflow
版权声明:本文标题:javascript - PouchDb fetch all documents by Id matching regex - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741803839a2398380.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论