I seem to have an issue in retrieving a selected few documents from a PouchDB using startkey and endkey. I need to get the documents back with key starting with "profile" (in this example profile41 & profile48).
When using the chrome PouchDB extension, this works fine when doing a query using startkey:"profile" and endkey:"profile\0ffff", but for some reason, this doesn't work when running my code (angular/ionic).
My code returns an empty doc list. When I set the startkey:"profile41" and endkey:"profile41" I do get the doc back, so I know it connects and can retrieve documents from the DB.
PS: First use of PouchDB, so I might have overlooked something simple here.
Some documents in my db
{
"_id": "animaltypes",
"_rev": "7-e413c314272a62a6a14ed293f5f934cf",
"value": {
"rev": "7-e413c314272a62a6a14ed293f5f934cf"
},
"key": "animaltypes"
}
{
"_id": "profile41",
"_rev": "3-f4065b825d304d79479e3576409ce744",
"value": {
"rev": "3-f4065b825d304d79479e3576409ce744"
},
"key": "profile41"
}
{
"_id": "profile48",
"_rev": "3-5e62a6e33f022a8ac30d46b80126dedd",
"value": {
"rev": "3-5e62a6e33f022a8ac30d46b80126dedd"
},
"key": "profile48"
}
My javascript that retrieves docs
this.getData = function(keystart,keyend){
var deferred = $q.defer();
localDB.allDocs({include_docs: true,
descending: true,
startkey:keystart,
endkey:keyend}, function(err, doc) {
if (err) {
deferred.reject(err);
} else {
var rows = [];
for (var x in doc.rows) {
rows.push(doc.rows[x].doc.data);
}
deferred.resolve(rows);
}
});
return deferred.promise;
};
This returns an empty array with the following params
startkey = "profile"
endkey = "profile\0ffff"
This returns the correct single doc when parameters are set to
startkey = "profile41"
endkey = "profile41"
If you want to use "descending:true" you have to change the order of startkey and endkey.
Indeed, if you don't use "descending:true" you B-Tree looks like that:
In result you will have 2 records
If you use "descending=true" you B-Tree looks like that:
in result you will have 0 record.