LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

[Question]Update data without deletion

Open fcin opened this issue 9 years ago • 3 comments

Hello, I load my data in chunks, because I don't want to keep entire collection in memory. The problem is that the database stores only the last chunk. I assume this is because whenever I do Update it replaces the collection. How can I insert more data into an existing collection?

This is the code I use:

public bool Add(T document)
        {
            using (var db = DbLite.GetDatabase())
            {
                var documents = db.GetCollection(typeof(T).Name);

                var bsonDoc = db.ToDocument(document);

                if (documents.FindById(document.Id) != null)
                    return documents.Update(db.ToDocument(document));
                
                documents.Insert(bsonDoc);
            }

            return true;
        }

fcin avatar Feb 26 '17 14:02 fcin

Hi @fcin, try:

public bool Add(T document)
{
    using (var db = DbLite.GetDatabase())
    {
        return db.GetCollection<T>(typeof(T).Name).Upsert(document);
    }
}

mbdavid avatar Feb 26 '17 18:02 mbdavid

Turns out I had 2.0.4 version, that's why I couldn't find Upsert method. Thank you. It certainly helped.

The only problem is that now I get NullReferenceException in checking if collection exists in other method This seems to be a very simple code, so I don't think I messed it up, Could you take a look at it? @mbdavid I don't really know what could be the cause of it. I tried to follow the stacktrace, but I don't it doesn't look like it's my code's fault.

public IEnumerable<T> GetAll()
        {
            using (var db = DbLite.GetDatabase())
            {
                if (!db.CollectionExists(typeof (T).Name)) // Throws NullReferenceException
                    return null;

                var documents = db.GetCollection(typeof(T).Name);
                return documents.FindAll().Select(x => (T)db.Mapper.ToObject(typeof(T), x)).ToList();
            }
        }
System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=LiteDB
  StackTrace:
       at LiteDB.StreamExtensions.<>c__DisplayClass4_0.<TryLock>b__0()
       at LiteDB.FileHelper.TryExec(Action action, TimeSpan timeout)
       at LiteDB.StreamExtensions.TryLock(FileStream stream, Int64 position, Int64 length, TimeSpan timeout)
       at LiteDB.FileDiskService.Lock(LockState state, TimeSpan timeout)
       at LiteDB.LockService.LockShared()
       at LiteDB.LockService.Shared()
       at LiteDB.LiteEngine.GetCollectionNames()
       at LiteDB.LiteDatabase.CollectionExists(String name)
       at DrawMe.Core.Repositories.DmRepository`1.GetAll() in F:\Obrazki\C#\DrawMe\DrawMe\DrawMe.Core\Repositories\DmRepository.cs:line 50
       at DrawMe.Core.Repositories.CachedDmRepository`1.GetAll() in F:\Obrazki\C#\DrawMe\DrawMe\DrawMe.Core\Repositories\CachedDmRepository.cs:line 62
       at DrawMe.BasicDocumentExplorer.ViewModels.DocumentListExplorerViewModel.Update() in F:\Obrazki\C#\DrawMe\DrawMe\DrawMe.BasicDocumentExplorer\ViewModels\DocumentListExplorerViewModel.cs:line 49
       at DrawMe.BasicDocumentExplorer.ViewModels.DocumentListExplorerViewModel..ctor(IDmRepository`1 dmRepository, IEventAggregator eventAggregator, ILanguageManager langManager) in F:\Obrazki\C#\DrawMe\DrawMe\DrawMe.BasicDocumentExplorer\ViewModels\DocumentListExplorerViewModel.cs:line 44

InnerException:

fcin avatar Feb 26 '17 19:02 fcin

Nevermind, I was disposing it, that's why I got NullReferenceException. This issue can be closed.

fcin avatar Mar 08 '17 08:03 fcin