[BUG] Predicates with arguments do not work with DeleteMany
See original GitHub issueVersion LiteDB 5.0.1, net45, Windows 10
Describe the bug
Predicates with arguments do not work with collection’s DeleteMany
in LiteDB v5.0.1
Code to Reproduce Example:
- create
BsonExpression
predicate with an argument - use it with
DeleteMany
, the expected document is not deleted - the same predicate works fine with
FindOne
DeleteMany
still works with the similar predicate without arguments
using (var db = new LiteDatabase(":memory:"))
{
var c1 = db.GetCollection("Test");
var d1 = new BsonDocument() { ["_id"] = 1, ["p1"] = 1 };
c1.Insert(d1);
Console.WriteLine($"count {c1.Count()}");
{
// try BsonExpression predicate with argument - not deleted
var e1 = BsonExpression.Create("$._id = @0", 1);
var r1 = c1.DeleteMany(e1);
Console.WriteLine($"1. deleted {r1}");
// the same BsonExpression predicate works fine in FindOne
var r = c1.FindOne(e1);
Console.WriteLine($"document: {r}");
}
{
// try BsonExpression predicate hardcoded - deleted
var e1 = BsonExpression.Create("$._id = 1");
var r1 = c1.DeleteMany(e1);
Console.WriteLine($"2. deleted {r1}");
// double check - Count = 0
Console.WriteLine($"count {c1.Count()}");
}
}
Output:
count 1
1. deleted 0
document: {"_id":1,"p1":1}
2. deleted 1
count 0
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (7 by maintainers)
Top Results From Across the Web
node.js - deleteMany() in mongodb not deleting documents
1 Answer. According to the documentation, deleteMany expects filter query as the first argument. In your case, since the first argument does ...
Read more >db.collection.deleteMany()
A document or string that specifies the index to use to support the query predicate. The option can take an index specification document...
Read more >db.collection.deleteOne()
A document or string that specifies the index to use to support the query predicate. The option can take an index specification document...
Read more >Deleting many rows from a big table - Ask TOM - Oracle
Tom: We have a 6 millons rows table and we need to clean it. This process will delete 1,5 millons. My first approach...
Read more >Delete by query API | Elasticsearch Guide [8.9]
Parameters like requests_per_second and max_docs on a request with slices are distributed proportionally to each sub-request. Combine that with the point above ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This will be very important when LiteDB will support some “T-SQL-Like” language for store procedures and triggers (is on my roadmap)
Hi @mbdavid , this method is from DAL (repo) implemented in linq way Doesnot work
public void DeleteByIds(IEnumerable<int> ids) { Collection.DeleteMany(v => ids.ToList().IndexOf(v.Id) > -1); }
I’m thinking it’s a bug but not sure (may ne should not be implemented on the db level) I had something like this and my solution was `public static class RepoBasicExt { public static IEnumerable<T> FindWithBug<T>(this RepoBasic repo, ILiteCollection<T> Collection, Expression<Func<T, bool>> func) { try { return Collection.Find(func); } catch (NotSupportedException ex) { var internalFunc = func.Compile(); return Collection.FindAll().Where(internalFunc); } } //not good if we talk about performance, may be make sense to think about it on the “db level” public static IEnumerable<T> Find<T>(this RepoBasic repo, ILiteCollection<T> Collection, Expression<Func<T, bool>> func) => Collection.FindAll().Where(func.Compile());