question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Predicates with arguments do not work with DeleteMany

See original GitHub issue

Version 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:closed
  • Created 4 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
mbdavidcommented, Feb 6, 2020

This will be very important when LiteDB will support some “T-SQL-Like” language for store procedures and triggers (is on my roadmap)

0reactions
qart2003commented, Jan 11, 2022

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());

}`
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found