CBL .NET 2.0.0-db016 Query exclude .IsDeleted documents

I was wondering how to query for documents that are not flagged for deletion. I could iterate the result but is there a direct approach

My best guess was

Expression.Met().IsDeleted

Full code:

   using (var database = new Database(DbName))
    {
        using (var query = Query.Select(SelectResult.Expression(Expression.Meta().ID))
                   .From(DataSource.Database(database))
                   .Where(Expression.Meta().ID.In(ids)))
                  
        {
            using (var results = query.Run())
            {
                database.InBatch(() =>
                {
                    var returnedIds = results?.Select(x => x.GetString("id")).ToList() ?? null;
                    if (returnedIds != null)
                        returnedIds.ForEach(id => documents.Add(database.GetDocument(id)));
                });
            }
        }
    }

It’s actually a bug that queries return deleted documents. The fix will be in the next DB.

(I’ve considered whether there should be an option to be able to filter them out yourself, but I suspect there are few cases where people will want to search deleted documents … if you have a need to, let me know.)

1 Like

Exactly this is what I think too. _deleted is a kind of soft delete in the system. But what if I want to purge all tombstone documents to save space ?

2 Likes

That’s a very good point, actually — there needs to be a way to find deleted docs in order to purge them.

Database has an allDocuments method but unlike 1.x it has no options for things like whether or not to include deleted documents. I’ll file an issue.