Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pouch DB PUT doc insert issue - question #8446

Closed
Dinesh101041 opened this issue Jan 29, 2022 · 5 comments
Closed

Pouch DB PUT doc insert issue - question #8446

Dinesh101041 opened this issue Jan 29, 2022 · 5 comments

Comments

@Dinesh101041
Copy link

Issue

i am working on small form in electron and pouch db

when we inserting a data to db , until the nineth doc it inserting perfectly , but on 10 the doc it is inserting and second position of doc instead on inserting into last

images :

form ui

Screenshot 2022-01-29 at 7 25 30 PM

before inserting the 10th doc

Screenshot 2022-01-29 at 7 27 07 PM

After inserting 10 th doc

Screenshot 2022-01-29 at 7 26 26 PM

here attached the js code

// form submit
const form = document.getElementById("form1");
form.addEventListener("submit", dbsubmit);

function dbsubmit(e) {
   e.preventDefault();
   //   getting values from form
   var Sno = document.getElementById("number").value
   var date = document.getElementById("date").value;
   var Time = document.getElementById("time").value;
   var Trip = document.querySelector('input[name="Trip"]:checked').value;
   var TripType = document.querySelector('input[name="Type"]:checked').value;
  


   // assigning form values to db table names
   var doc = {
       _id: Sno,
       Date: date,
       time: Time,
       trip: Trip,
       triptype: TripType,
   };

   // inserting to db
   db.put(doc, function(err, response) {
       if (err) {
           return console.log(err);
       } else {
           console.log("Document created Successfully", response);
           }
   });

}

inserting until nineth 9th doc its perfectly inserting
but when we insert the 10 th doc it is inserting in second position

i debuged , but cannot able to find solution can anyone help me with the solution

Thank you

Info

  • Environment: Electron js
  • Adapter: leveldb
  • Server: PouchDB Server
@ramblin-rose
Copy link

What you are observing is correct. This is explained thoroughly in the CouchDB documentation 3.2.2.5. Collation Specification

Comparison of strings is done using ICU which implements the Unicode Collation Algorithm, giving a dictionary sorting of keys. This can give surprising results if you were expecting ASCII ordering.

emphasis mine

A thorough read of that section will provide a solid grasp of the collation rules.

@Dinesh101041
Copy link
Author

What you are observing is correct. This is explained thoroughly in the CouchDB documentation 3.2.2.5. Collation Specification

Comparison of strings is done using ICU which implements the Unicode Collation Algorithm, giving a dictionary sorting of keys. This can give surprising results if you were expecting ASCII ordering.

emphasis mine

A thorough read of that section will provide a solid grasp of the collation rules.

any way to solve this i cant able to find the correct way to sort the numbers in correct order can you help me with that

@ramblin-rose
Copy link

ramblin-rose commented Jan 30, 2022

Best asked on https://stackoverflow.com

edit: However:

find the correct way to sort the numbers

But the field key is not a number type, it is a string type. That should make everything clear.

@Dinesh101041
Copy link
Author

Best asked on https://stackoverflow.com

edit: However:

find the correct way to sort the numbers

But the field key is not a number type, it is a string type. That should make everything clear.

want to convert the key from string to int ?

@janl
Copy link
Contributor

janl commented Feb 1, 2022

In PouchDB, document ids must be strings, they can not be numbers.

In order to do what you want, you need to create a view with a map function that looks like this:

function (doc) {
  emit(Number(doc._id)
}

this will give you a view query result that is sorted by the number value of the document _id

@janl janl closed this as completed Feb 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants