de albuquerque frank
de albuquerque frank

Reputation: 197

Queries error sorting documents with find Pouchdb

I wish to order/sort results with pouchdb. I have created an index on the field i wish to sort by greater than 882

I've checked that the database exist

Then the result is like :

883

895

9

909

917

93

I am refering to the documentation : https://pouchdb.com/guides/mango-queries.html and this documentation : http://docs.couchdb.org/en/stable/api/database/find.html

    this.db= new PouchDB('parcelles', {adapter: 'idb'});

        // Create an index to the field id_culture

            this.db.createIndex({
             index: {
             fields: ['id_culture']
              }
              }).then((result)=> {
                      console.log(result);

              }).catch(function (err) {

              });

        // Query with the sort filter

              this.db.find({
              selector: {
              id_culture: {$gte: '882'}
               },
              sort: ['id_culture']
               }).then( (result)=> {console.log(result);

               }).catch(function (err) {console.log(err);

               });

Upvotes: 0

Views: 288

Answers (1)

Martin Bramwell
Martin Bramwell

Reputation: 2121

Your attribute id_culture is a text string not a number. You will have to decide on a maximum possible size, for example 100,000,000 and left pad all your ids.

I recommend prefixes as well, so you could try, for example: Culture_00000009, Culture_00000909, Culture_00000093, etc. With ids like that sorting will work....

id_culture: {$gte: 'Culture_00000882'}

... giving ...

Culture_00000883
Culture_00000895
Culture_00000909
Culture_00000917
Culture_00001093

Upvotes: 1

Related Questions