4

I'm learning how to use LiteDB. Here's a bit of C# code I use to insert a .Net object into the database:

     // Create new YacksProjectInfo object and insert it into the database
     YacksProjectInfo projectInfo = new YacksProjectInfo(AssemblyName, moduleNumber);
     yacksProjects.Insert(projectInfo);  // Id field generated automatically
     yacksProjects.EnsureIndex((YacksProjectInfo x) => x.ProjectName);
     yacksProjects.EnsureIndex((YacksProjectInfo x) => x.ModuleNumber);

In other words, I'm using the EnsureIndex() method every time I add a new object to the database. But I'm guessing this isn't necessary, that you only need to do it once after inserting the first object.

Can someone confirm if EnsureIndex() needs to be done every time, or if once is enough?

RenniePet
  • 11,420
  • 7
  • 80
  • 106

1 Answers1

7

I've found the answer here (and I'm slightly less embarrassed about asking a question that has a fairly obvious answer after finding two other people who asked the same question): https://github.com/mbdavid/LiteDB/issues/789

To quote:

"and do I need to call it EnsureIndex at every write operation?"

"I'm wondering the same thing. When exactly do you call EnsureIndex? Do you just use it the first time you add a new collection to a database to set up the indexes, or do you need to use it every time after calling GetCollection for example? The docs aren't very clear on this."

Answer by @mbdavid, who created LiteDB:

"you need call EnsureIndex before run a query to database create index IF NEEDED. If index already created (using same EnsureIndex), EnsureIndex will do nothing. So, to be pratic, be best place to create all your indexes are in database initialization"

Incidentally, I also learned something else in playing around with EnsureIndex() - it has a second parameter "unique", a bool value. So I'm guessing that setting this true (when relevant) improves efficiency a bit and also prevents inserting a new document with a non-unique index.

So now my creating of the LiteDB database collection looks like this:

        // Get the collection (or create, if doesn't exist)
        LiteCollection<YacksProjectInfo> yacksProjects = 
                                  liteDatabase.GetCollection<YacksProjectInfo>("YacksProjects");

        // Index current and future documents using ProjectName and ModuleNumber properties
        yacksProjects.EnsureIndex((YacksProjectInfo x) => x.ProjectName, true);
        yacksProjects.EnsureIndex((YacksProjectInfo x) => x.ModuleNumber, true);
RenniePet
  • 11,420
  • 7
  • 80
  • 106
  • 4
    This method name EnsureIndex is not a good name to used and create this confusion, but I copy this from MongoDB. Now, MongoDB deprecated this method and use "CreateIndex" insted - it's easy to understand and do same thing. – mbdavid Mar 12 '18 at 00:50
  • @mbdavid Thanks for your comment, and thank you very much for creating LiteDB and making it available. So you'll be adding a CreateIndex() method in a future version of LiteDB? – RenniePet Mar 12 '18 at 04:11
  • 1
    Thanks for using LiteDB. I'm working on a big next version (v5) with many new features, including new index system. Than, I will update as MongoDB do and will define EnsureIndex as obsolete. – mbdavid Mar 13 '18 at 09:31