mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.62k stars 1.25k forks source link

[BUG] Upsert Returns always False #2173

Open CatsWithoutDirection opened 2 years ago

CatsWithoutDirection commented 2 years ago

Version 5.0.1 Describe the bug Upsert Returns always False while it upsert successfully Code to Reproduce MakerSureConfigTypeHasNoId(typeof(TConfig)); var result = Db.GetCollection(typeof(TConfig).Name).Upsert(SingleId, config); Console.WriteLine($"Upsert Result:{result}"); return result; Expected behavior i hope it could return write return Screenshots/Stacktrace image

Additional context There is another problem that when i use the method several times continuously ,it wont work for some "Set" except the first

yueyinqiu commented 1 year ago

Also happened to me. After checking the code, I found this in LiteDB/Engine/Engine/Upsert.cs:

                foreach (var doc in docs)
                {
                    transaction.Safepoint();

                    // first try update document (if exists _id), if not found, do insert
                    if (doc["_id"] == BsonValue.Null || this.UpdateDocument(snapshot, collectionPage, doc, indexer, data) == false)
                    {
                        this.InsertDocument(snapshot, doc, autoId, indexer, data);
                        count++;
                    }
                }

The updated documents are not included when counting. (And LiteCollection.Upsert just compared the result to 1 so it returns false.)

So this might be intentionally designed. But I still think it is highly misleading, especially when the document says nothing about the result.