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.