louischatriot / nedb

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

How add new colum if the colum dosn't exist ? #658

Open Thalinda opened 3 years ago

Thalinda commented 3 years ago

Hello i am developing application witch use the nedb (by the way it's great) now the issue is i have added some new fields to the database but the previous code don't have in the this fields in the database is there any way to add this filed in nedb and i can't clean slate the database since there can be 100 of files already there since user using the app right now

jdpacini commented 3 years ago

If a field doesn't exist, NEDB will create it automatically (e.g. db.update({'foo.bar': 'value'}) will create a field of "bar" inside "foo").

Thalinda commented 3 years ago

OK but now my issues is i need to check if the filed exist before reading is there any way since my code stop from there when it's didn't find the field. And thank you very much for quick response

jdpacini commented 3 years ago

Maybe there is a better way to handle this, but without updating your database, you can provide a default value like let x = db.find({key: value}) || value;

I run into the same problem a lot too so I'm also interested to know if anyone has a better solution.