Code Monkey home page Code Monkey logo

Comments (11)

louischatriot avatar louischatriot commented on July 21, 2024

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.

 avatar commented on July 21, 2024

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.

louischatriot avatar louischatriot commented on July 21, 2024

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.

 avatar commented on July 21, 2024

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.

louischatriot avatar louischatriot commented on July 21, 2024

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.

 avatar commented on July 21, 2024

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.

louischatriot avatar louischatriot commented on July 21, 2024

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.

 avatar commented on July 21, 2024

and just wringing the topic to it's death - is there any need or value in indexing the _id column??

from nedb.

louischatriot avatar louischatriot commented on July 21, 2024

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.

 avatar commented on July 21, 2024

So I need to create it or is it automatically created!?

from nedb.

louischatriot avatar louischatriot commented on July 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.