I've updated LiteDB to version 4.
As I understand since v.4 I need to take care of indexes by myself in case of insert/delete documents. I use the repository pattern, since it easy and confirm with current project's needs. How can I call EnsureIndexes (or whatever to rebuild index) without quering collection?
I'm new to LiteDB, sorry if this question has been covered already in manual or another discussion.
Hi @pylnik, I always recommend to use EnsureIndex in your initialize database. If you need schema change, you can have a single place to update your data. Take a look here:
Hi @mbdavid ! Thank you for the prompt reply.
I've seen this method in LiteDatabase::Engine but hoped to omit using strings for collection name. In repository pattern it's perfect hidden under template (e.g. LiteRepo.Insert<T>(tp); ).
Is there any possibility not to use string in EnsureIndex(string collection,...) ? For example use strong-type collection or obtain collection name somehow from entity we used to insert.
Yes, you can use LiteCollection for typed class
var col = db.GetCollection<YourClass>(); // Collection name is typeof(T).Name
col.EnsureIndex(x => x.Name)
LiteEngine is lower data layer to access. There is no strong types in LiteEngine, only strings (for collection name) and BsonDocument as document. Only in LiteDatabase (and LiteRepository) is possible map strong type to BsonDocument.
Thank you for the support.
Once again to clarify: I do need to perform EnsureIndex even if the index has been built only for Id property, don't I? It means, there is no automatic index calculation at all?
Hi @pylnik, there no need create index to your Id property (Bson _id field) because they already created when you insert your first document. This is the only auto-create index: PK index.
Most helpful comment
Hi @pylnik, there no need create index to your Id property (Bson
_idfield) because they already created when you insert your first document. This is the only auto-create index: PK index.