Documents Updated for a 2nd time do not get saved/updated

Hi,

I have this issue where i am trying to update a document. The first time i attempt to update the document everything goes flawless. However, after i want to make a 2nd update the value remains the same after saving to the database.

here is my code:

public void CreateSingleDocumentFromDictionary(IDictionary<string, object> info, bool update = false)
    {
        using (var database = new Database(DbName))
        {
            object value;
            if (info.TryGetValue("key", out value))
            {
                if (database.GetDocument(value.ToString()) == null || update == true)
                {
                    var document = new MutableDocument(value.ToString(), info);
                    database.Save(document);
                    if (database.GetDocument(value.ToString()) != null)
                    {
                        Task.Run(() => Logger.Info(String.Format("DOCUMENT Created: id {0}", value.ToString())));
                    }
                    var tst = database.GetDocument(document.Id);
                    var psd = tst.GetObject("purchasedetails");
                }
                else
                {
                    Logger.Error(null, "id already in database", (object)JsonConvert.SerializeObject(info));
                    throw new NoKeyPropertyException(String.Format("Id already in db in object(s) during 
document creation"));
                }
            }
            else
            {
                Logger.Error(null, "No key found in object(s)", (object)JsonConvert.SerializeObject(info));
                throw new NoKeyPropertyException(String.Format("No key found in object(s) during document 
 creation"));
            }
        }
    }

where if “psd” is a dictionary of a list of objects. The update i made is in an object in that list.

I don’t realy know what im doing wrong here. Im using Couchbase Lite version 2.0.0 db-020.

me and my colleague @thomas1983 have been breaking our heads on this for the entire afternoon.

I checked the documentation about this and it says that sometimes changes might not get through due to multiple instances of the database. Here, I check the results of my “update” in the same instance.

any suggestions on how this might be caused are welcome.

cheers.

1 Like

You can’t update an existing document by creating a new empty MutableDocument instance and saving it. That creates a conflict.

Instead, you get the existing document, make a mutable copy, modify the copy, then save it.

1 Like

Hi Jens,

Indeed your approach works!

doing it like this solved the problem for me. I created a new method specifically for saving.

public void UpdateSingleDocumentFromDictionary(IDictionary<string, object> info)
        {
            using (var database = new Database(DbName))
            {
                object value;
                if (info.TryGetValue("key", out value))
                {
                    var document = database.GetDocument(value.ToString()).ToMutable();

                    foreach (var entry in info)
                    {
                        document.Set(entry.Key, entry.Value);
                    }

                    database.Save(document);

                    var loadedDocument = database.GetDocument(value.ToString());

                    if (loadedDocument != null && loadedDocument.Keys.Count > 0)
                    {
                        Task.Run(() => Logger.Info(String.Format("DOCUMENT updated: id {0}", value.ToString())));
                    }

                }
                else
                {
                    Logger.Error(null, "No key found in object(s)", (object)JsonConvert.SerializeObject(info));
                    throw new NoKeyPropertyException(String.Format("No key found in object(s) during document creation"));
                }
            }
        }