Search code examples
c#nosqlejdb

EJDB C# Binding


I'm having some trouble with the C# Binding for EJDB.

It's propably just an understanding issue.

Well I want to use EJDB to store some very basic Data. This Data shall be available whenever I start my program. (persistent)

I get no error running the code. It skips over the foreach at the end as the query.Find() returns nothing. If you have a look at the comments, you see i do a query on "myCollection" twice. Once after I inserted data and once later in the second method. The first count returns 1 and the second count returns 0. Indicating that there must be some datawipe between those two methods. My guess is the Dispose method, though if i do not call this the db does not get closed and i get exceptions when i try to open it again.

Any help would be greatly appreciated. Is EJDB only for Runtime data? Or do i have to save the DB somehow? Like a commit. Or do I close the DB wrong?

Now my code looks kinda like that:

    static public Data createNewData(Data myData)
    {
        var myDB = new EJDB("MyDB", EJDB.DEFAULT_OPEN_MODE | EJDB.JBOTRUNC);
        myDB.ThrowExceptionOnFail = true;

        var data = BSONDocument.ValueOf(new
        {
            name = myData.Name,
            guiName = myData.GuiName
        });

        myDB.Save("myCollection", data);
        //update ID
        myData.m_ID = data["_id"].ToString();

        //returns 1, => it worked
        int count = myDB.CreateQueryFor("myCollection").Count();

        //close the DB (as in the example, maybe thats the error? but then how to close the DB?)
        myDB.Dispose();

        //now calling the second method where the DB is empty again
        AllData.updateData();

        return myData;
    }


    static internal void updateData()
    {
        var myDB = new EJDB("MyDB", EJDB.DEFAULT_OPEN_MODE | EJDB.JBOTRUNC);
        myDB.ThrowExceptionOnFail = true;

        //just for testing
        //returns 0 DB seems to be empty, but i just stored the data in the previous method?!
        int count = myDB.CreateQueryFor("myCollection").Count();

        //get all data stored in myCollection
        var query = myDB.CreateQueryFor("myCollection");

        //this always finds nothing. the db seems to be empty
        using (var cur = query.Find())
        {
            //this foreach gets skipped as there is cur is empty
            foreach (var e in cur)
            {
                BSONDocument rdoc = e.ToBSONDocument();
                Data newData = Data.createNewDataFromBSONDocument(rdoc);
                AllData.Add(newData);
            }
        }

        myDB.Dispose();
        query.Dispose();
    }

Solution

  • Oops i found the error. shame on me One shall not copy paste from the example without thinking.

    It was the Option EJDB.JBOTRUNC when opening the DB which just deletes all previous Data...