Do you plan on adding upserting methods like in MongoDB ?
Like in this example :
Foo foo = ...
var replaceOneResult = await collection.ReplaceOneAsync(
doc => doc.Id == foo.Id,
foo,
new UpdateOptions {IsUpsert = true});
Much thanks for this useful library.
I've been working with a basic extension method to take the place of this behaviour until such a time when it is truly available. Here's a simple implementation which relies on the type having an ID field that LiteDB can map correctly, or a mapping entry for the type in question that handles keying said objects. If the keys are equal, the item will be upserted.
In short, it's really an Update call followed by an Insert call on failure, but it works for me in cases where performance aren't super critical.
Implementation available here
I see, seems to be a good option too. Thanks !
Remember, performance isn't the greatest and it isn't atomic, so it can be iffy in situations with multiple simultaneous data sources.
For better performance, I suggest convert your T value to BsonDocument once (in this solution, will be run twice). But atomic operation only implementing in DbEngine.cs.
I will add this functionality to 2.1 version, because now I'm trying to only bug fix to v2.0. The idea to 2.1 will me has operations with update, like atomic FindAndModify and expressions like my_number: { $inc: 1 }
When will you update to v.2.1.0? I can't wait to enjoy it!
Just adding my :+1: to this request; it's a really useful feature when designing idempotent workloads.
My current 'workaround' (which is really more of pretty code than efficient):
collection.InsertBulk(documentsToUpsert.Where(d => !collection.Update(d)).ToList());
It's done in v3-beta
@mbdavid you added it to the engine, but not the Collection?
ops, I missing add in LiteCollection.... I will add soon
Most helpful comment
Just adding my :+1: to this request; it's a really useful feature when designing idempotent workloads.
My current 'workaround' (which is really more of pretty code than efficient):
collection.InsertBulk(documentsToUpsert.Where(d => !collection.Update(d)).ToList());