nedb icon indicating copy to clipboard operation
nedb copied to clipboard

find({}) scrambles data file

Open alexHunter854 opened this issue 3 years ago • 1 comments

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.

alexHunter854 avatar Jan 10 '21 15:01 alexHunter854

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().

jampy avatar Jan 11 '21 08:01 jampy