Comments (11)
Hello,
You can actually use ensureIndex
whenever you want:
- Even before you call
loadDatabase
- Right after a
loadDatabase
- After a few inserts, updates, removes
The index gets created right away, and is initialized with whatever data is in the database when you call ensureIndex
. Also, calling ensureIndex
a second time on the same key has no effect since the index was already created. That means you never need to check whether an insert is the first one or not. From the moment you call ensureIndex
, you get the speed boost.
Note that index creation will fail if you try to create a unique index and the existing data violates it (which is the expected behaviour).
Indexes are in-memory and exist only as long as your application is open. They get recreated everytime you reload your app (but this is very quick). That means that if you want to stop using an index, you simply need to remove the corresponding call to ensureIndex
from your code and reload your app.
This should answer your questions :) If not please reopen the issue.
from nedb.
The error I get if I just create a new database and attempt to add an index is
"Uncaught TypeError: Cannot call method 'hasOwnProperty' of undefined " in datastore.js 207
If I create a document first, the error disappears - so I'm assuming it related to an 'empty' collection (sorry, I'm a SQL man so keep wanting to say 'row' and 'table' - not upto speed on the lingo yet!)
from nedb.
Hmm, that seems indeed weird. Could you tell what version of nedb you're using ? Or even better, could you update to the latest version (v0.7.5), try again, and tell me if ans where the error is thrown ? Also, could you copy paste a simple snippet of code here that allows me to reproduce the bug on nedb's latest version ?
from nedb.
I downloaded (via npm) yesterday - checked it today and it's 'current' - and thanks for the info on how indexes work.
I think the error was actually coming from something else (things being async makes it hard to debug - as I'm replacing Web SQL code it's not ideal!) - I actually had a
db.find({"name": name},handler());
and name was 'undefined' - and THAT is what is causing the error above - easily fixed, tho a more graceful crash might be nice... ;)
from nedb.
That's great to hear :) That said you're right, no error should be raised when you query using undefined. I just fixed this and published the new version (v0.7.6).
from nedb.
Waking this with a question - is it OK to update the values (in the key) which are reflected in the index or do I need to re-create the index thereafter??
from nedb.
You you create an index on a field it will be maintained, so you don't need to worry about anything, just use the usual commands. Even if you change the key, the index structure will be reorganized automatically to reflect that.
from nedb.
and just wringing the topic to it's death - is there any need or value in indexing the _id column??
from nedb.
It is both needed by NeDB (it needs to have at least one index to organize data) and has value (it is expected that calls to find
using a query on _id are the fastest)
from nedb.
So I need to create it or is it automatically created!?
from nedb.
The one on _id is automatically created, ne need to do it. If you called ensureIndex on the _id field it's not an issue though : if the index already exists it has no effect.
from nedb.
Related Issues (20)
- NPM found 2 high severity vulnerabilities in NeDB, require manual review HOT 10
- NeDB datastore Model needs to be provided
- update the version of underscore dependence! HOT 1
- How to search multiple feilds' "$and"? HOT 4
- Add link to "official" fork HOT 8
- "this library is no longer maintained" HOT 7
- Insert doesn't take place immediately HOT 2
- How to catch abort if the capacity of storage is full??
- $setOnInsert is not work??????
- high vulnerabilities for this package HOT 2
- Error: Cannot create a string longer than 0x1fffffe8 characters HOT 1
- Please update dependency underscore HOT 4
- files
- Data Loss HOT 1
- Request to move repo under NW Userland
- PLEASE !!! Fork this project and create a new one HOT 1
- How to use expireAfterSeconds?
- i have a probleme to photo to nedb db HOT 1
- Regarding the issue of writing permissions
- Update creates a duplicate with exact same _id HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nedb.