With version 4.X I was able to delete entries with:
col.Delete(Query.EQ("_id", toDelete.Id));
This doesn't work with 5.X. So what is the best approach with 5.X to delete records? I didn't find anything in the documentation?
Hi @gnimor
Here's a sample that (hopefully answers your question.
class Program
{
static void Main(string[] args)
{
using var db = new LiteDatabase(new MemoryStream());
var col = db.GetCollection<TestClass>();
var instance = new TestClass
{
Id = Guid.NewGuid(),
Data = "My data"
};
col.Insert(instance);
Console.WriteLine($"{nameof(TestClass)} collection contains {col.Count()} item(s)");
Console.WriteLine($"Deleting item by id in collection {nameof(TestClass)}");
// Delete specific item by id
col.Delete(instance.Id);
Console.WriteLine($"{nameof(TestClass)} collection contains {col.Count()} item(s)\n");
// Inserting item again
col.Insert(instance);
Console.WriteLine($"{nameof(TestClass)} collection contains {col.Count()} item(s)");
Console.WriteLine($"Deleting item by id in collection {nameof(TestClass)}");
// Delete using predicate lambda
col.DeleteMany(item => item.Data == "My data");
// or delete using Bson expressions
col.DeleteMany(Query.StartsWith(nameof(instance.Data), instance.Data));
Console.WriteLine($"{nameof(TestClass)} collection contains {col.Count()} item(s)");
}
class TestClass
{
public Guid Id { get; set; }
public string Data { get; set; }
}
}
As you can see in the sample, you could either delete a specific item by it's Id using the col.Delete(<id>) method, or you could use the DeleteMany method which accepts predicates and BSON expressions.
If this answers your question, please close the issue 馃檪
This works as expected
Most helpful comment
Hi @gnimor
Here's a sample that (hopefully answers your question.
As you can see in the sample, you could either delete a specific item by it's Id using the
col.Delete(<id>)method, or you could use the DeleteMany method which accepts predicates and BSON expressions.If this answers your question, please close the issue 馃檪