LiteDB: Invalid BSON data type 'Null' on field '_id'
<p>Using <a href="http://litedb.org" rel="noreferrer">LiteDB</a>, and it is amazing. It works well for loading and storing data, however, not on subsequent loads after the database has been created.</p>

<p>On initial load, everything is perfect. It creates the database and stores the new record flawlessly, and query returns empty since nothing exists in that collection yet.</p>

<p>On subsequent load, after querying the data (works and gets the result), there is a problem in the <code>.Update()</code> which is causing this issue. According to their documentation, when an 'Id' is not specified it is supposed to create one. When the object is returned from the collection, it does not contain this '_Id' field, and thus fails to update the record in the database.</p>

<pre><code>public class AuctionCache
{
    public double lastModified { get; set; }
    public string server { get; set; }
    public AuctionCache() { }
}

private static bool IsCached(AuctionCache auction)
{
    string filename = string.Format("{0}-{1}.json", auction.server, auction.lastModified);
    bool cached = false;
    try
    {
        using (LiteDatabase db = new LiteDatabase("cache.db"))
        {
            // Get customer collection
            var auctions = db.GetCollection&lt;AuctionCache&gt;("auctions");

            // Use Linq to query documents
            try
            {
                var results = auctions.Find(x =&gt; x.server == auction.server).DefaultIfEmpty(null).Single();
                if (results == null)
                {
                    // Insert new cached server
                    auctions.Insert(auction);
                    auctions.EnsureIndex(x =&gt; x.server);
                }
                else
                {
                    if (results.lastModified &lt; auction.lastModified)
                    {
                        // Update existing cached server data
                        results.lastModified = auction.lastModified;
                        auctions.Update(results);
                        auctions.EnsureIndex(x =&gt; x.server);
                    }
                    else
                    {
                        cached = File.Exists(filename);
                    }
                }
            }
            catch (LiteException le1) {
                Log.Output(le1.Message);
                // Get stack trace for the exception with source file information
                var st = new StackTrace(le1, true);
                // Get the top stack frame
                var frame = st.GetFrame(0);
                // Get the line number from the stack frame
                var line = frame.GetFileLineNumber();
                var module = frame.GetMethod();
                var file = frame.GetFileName();
            }
            catch (Exception e)
            {
                Log.Output(e.Message);
                // Get stack trace for the exception with source file information
                var st = new StackTrace(e, true);
                // Get the top stack frame
                var frame = st.GetFrame(0);
                // Get the line number from the stack frame
                var line = frame.GetFileLineNumber();
            }
        }
    } catch (Exception ee) {
        Log.Output(ee.Message);
        // Get stack trace for the exception with source file information
        var st = new StackTrace(ee, true);
        // Get the top stack frame
        var frame = st.GetFrame(0);
        // Get the line number from the stack frame
        var line = frame.GetFileLineNumber();
    }
    return cached;
}
</code></pre>

<p>If you wish to use the above code quickly, you can use the following for demonstration purposes (initial load) :</p>

<pre><code>   AuctionCache ac = new AuctionCache();
   ac.lastModified = (double)12345679;
   ac.server = "Foo";

   // should return true on subsequent loads.
   Console.WriteLine( IsCached( ac ) );
</code></pre>

<p>Make sure when testing to stop the program after initial creation, and then start the program 'fresh' for a clean test of loading / updating the database using the following code :</p>

<pre><code>   AuctionCache ac = new AuctionCache();
   ac.lastModified = (double)22345679;
   ac.server = "Foo";

   // should return true on subsequent loads.
   Console.WriteLine( IsCached( ac ) );
</code></pre>

<p>This should ensure that it will try to update the record and flag the problem in your IDE as <code>lastModified</code> is newer than the one stored in <code>cache.db</code> which will trigger the <code>.Update</code> method inside my <code>IsCached</code> method.</p>