Couchbase lite version 13: Purging documents from the database

Hi guys.

With the new release from Couchbase Lite the Document and DocumentID properties have been removed and replaced with a new IResult as return value. However To Purge (completely remove) a document from your database, the purge() method still requires a Document object. My question: is how do you obtain this object of type document from the IResult you get from the query you executed? Couchbase also announced that Document might make a return in the future.

Thanks in advance!

cheers.

You’re right, the document ID or document are not returned in the query result by default. You can use the meta function to get the document ID in the query result. See this example in Swift https://developer.couchbase.com/documentation/mobile/2.0/guides/couchbase-lite/native-api/query/index.html#meta-function

The same applies for .NET. From there you can get document object.

(I assumed you’re referring to the 2.0 API)

1 Like

I understand. But how can i access the metadata from the result when i run the query?

var query = Query.Select(SelectResult.Expression(Expression.Meta().ID))
                .From(DataSource.Database(settingsDatabase))
                .Where(Expression.Property("code").NotEqualTo(null))
                ;

            var rows = query.Run();

            foreach (var row in rows)
            {

this is where i would like to get the meta data and use it to get the document. but how do i access it?

            settingsDatabase.Purge(settingsDatabase.GetDocument("meta_id"));
                
            }

thanks :smiley:

1 Like

The query result is in the format specified in the SELECT clause. You can get that property by it’s name (row.string(forKey: "id")) or position in the SELECT clause (row.string(at: 0) in your example).

2 Likes

Alright ill check that out. :smiley:

I found a solution to problem.

public static List<Item> DeleteItem(Item item)       
    {
        List<Item> items = new List<Item>();
        using (var itemDatabase = new Database("item"))
        {
            var query = Query.Select(SelectResult.Expression(Expression.Property("key"))
                                    
            )
            .From(DataSource.Database(itemDatabase))
            .Where(Expression.Property("key").EqualTo(item.key))

            ;

            try
            {
                var rows = query.Run();

                if (rows != null)
                {
                    foreach (var row in rows)
                    {
                        var document = row;

                        itemDatabase.Purge(new Document(document.GetString(key:"key")));
                    }
                }

            }
            catch (Exception e)
            {
                var ex = e;
            }

        }


        return items;

    }

Curious - Is there a reason you are manually purging a document ? Couchbase Lite has built in logic to purge old and unused revisions . As you may probably know, purges are not replicated which means the document can reappear in your database through a pull replication for instance, if another client updates it.

Hi @priya.rajagopal,

I am currently testing the CRUD operations :smiley: so im still learning how it all works.
What is the correct way to remove a document from the database locally and server-side?

You mentioned that CBL automatically purges old & unused revisions. but what if i want to manually remove a document. for example a student has changed class. so he/she has to be removed (purged) from the database of students he/she is currently in and added into another. As fast as possible

Im just looking for a way to completely remove a document forgood.

i already managed to select documents and show them in a list. (these documents come from the server). so i got replication going. but still i sometimes get the same problem as before… see my other topic about this => replication failure

Any help is welcome! :smiley:

cheers!

Hi - Use delete instead of purge if you want to remove the document and have that change replicated. Delete will tombstone a revision so its marked as deleted. The JSON body will be removed.
If you would like to learn more about pruning and compaction, this blog may be a good read .

1 Like

Yes! i got it working thank you very much for the reply :smiley:

1 Like