louischatriot / nedb

The JavaScript Database, for Node.js, nw.js, electron and the browser
MIT License
13.46k stars 1.03k forks source link

find({}) scrambles data file #666

Open alexHunter854 opened 3 years ago

alexHunter854 commented 3 years ago

each time I do database.find({}) it scrambles up the datafile e.g.:

if we start with (data file name).db equaling to:

{"timestamp":0,"_id":"WCFTwmVybm6r0o9G"}
{"timestamp":1,"_id":"5E8twe59ulwJ9pck"}
{"timestamp":2,"_id":"s0eUV0Nt8JjOsHKO"}

and we make a find({}) request we end up with a (data file name).db that may equal to:

{"timestamp":2,"_id":"s0eUV0Nt8JjOsHKO"}
{"timestamp":0,"_id":"WCFTwmVybm6r0o9G"}
{"timestamp":1,"_id":"5E8twe59ulwJ9pck"}

given the small sample size of three it may come out as sorted but if the database is hundreds of entries long you will never get it in the right order this seems too puzzling to be intended behavior, I know you can fix this with sort() but let's say I can't use sort() for our purposes.

I really hope that this is not the intended behavior because it feels really really janky.

jampy commented 3 years ago

I guess that is per design, see auto-compacting: https://github.com/louischatriot/nedb#persistence

You might not expect the data to be persisted in any specific order. If you need a specific sorting, let the database sort it during find().