Pouchdb: Sort not working if the field(s) in the 'sort' is not available in all documents

Created on 19 Feb 2017  路  2Comments  路  Source: pouchdb/pouchdb

_From @nadinugraha on October 9, 2016 14:25_

I have documents with different type/structure of JSON in pouchdb. For example for ITEMS I have the following JSON structure:
{
"_id": "14f2e2dc-73a6-4cce-ba1a-27ce149caaa4",
"itemname": "Iphone 7",
"UOM": "unit",
"docType": "titems",
"_rev": "8-dd27c7573c888cd5000e3c54fefc2364"
}

On the other hand, for STORES I have the following JSON structure :
{
"_id": "15f5e2dc-73a6-4cce-ba1a-27ce149casax",
"storename": "Store 1",
"address": "Los Angeles",
"country": "USA",
"docType": "tstores",
"_rev": "9-ee27c7573c888cd6001e3c54fefc236e"
}

Both document structures share the same field that is "docType" to help me identify which document structure I am accessing. The problem happened when I do sorting. If I put "docType" in the sort then the resultset variable returned the documents as expected.

But when I changed the sort into "itemname", resultset is null or undefined. I wonder if field name that we put in sort must exist in all documents ? "docType" exists in all documents so that db.find could return value, while "itemname" only exists in some documents whose docType="titems" so that's why db.find returned null/undefined. Is my thought correct ?

                db.createIndex({
                    index: {
                                 fields: ['itemname','docType'],
                                 name: 'ItemIndex23'
                               }
                })
                .then(function (result) {
                    var cfgdoc;
                    if (result) {
                        return db.find({
                                    selector: {  docType: 'titems' },
                                    sort: [ 'docType']
                                })
                                .then(function(resultset) {
                                    cfgdoc = resultset;
                                    return cfgdoc;
                                })
                                .catch(function(err) {
                                    return err;
                                });
                    };
                })  

_Copied from original issue: nolanlawson/pouchdb-find#216_

bug

Most helpful comment

_From @numerized on December 13, 2016 22:16_

Hi there, I encounter the exact same behaviour.

All 2 comments

_From @numerized on December 13, 2016 22:16_

Hi there, I encounter the exact same behaviour.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings