Skip to content

Insert complex JObject failed with Exception:“Parameter count mismatch.” #96

@jabobian

Description

@jabobian

With simple JObject, all the codes run correctly.

       public async void InitData()
        {
            var json = @"{'_id': 12, 'array': [ 1, 2, 3 ], 'boolean': true, 'null': null, 'number': 123, 'object': { 'a': 'b', 'c': 'd', 'e': 'f' }, 'date': '2014-01-01T23:28:56.782Z', 'string': 'Hello World', 'emptyString': '', 'emptyObject': {}, 'emptyArray': [] }";
            JObject jObj = JObject.Parse(json);

            String pathToJson = "D:\\Temp\\test.json";
            var store = new DataStore(pathToJson);

            // Get employee collection
            var collection = store.GetCollection("employee");

            await collection.InsertOneAsync(jObj);

            jObj["array"][1] = 3;

            await collection.UpdateOneAsync(e => e._id == 12, jObj);
        }

But with Complex data structure, it fails.

public async void InitData()
{
    var ja = new JArray { "Hello World!", 77, true, 0.17, null };
    var jo = new JObject { ["_id"] = "jo", ["aString"] = "Hello World!", ["anInt"] = 77, ["aBool"] = true, ["aDouble"] = 0.17, ["aNull"] = null };
    
    var jObj = new JObject()
    {
        ["_id"] = 11,
        ["anArray"] = new JArray(ja),
        ["anotherArray"] = new JArray() { new JObject(jo), null, 1, "hi", new JArray(ja) },
        ["anObject"] = new JObject(jo),
        ["anotherObject"] = new JObject() { ["x"] = new JObject(jo), ["y"] = null, ["z"] = 1, ["a"] = "hi", ["b"] = new JArray(ja) },
    };
    
    String pathToJson = "D:\\Temp\\test.json";
    var store = new DataStore(pathToJson);
    
    // Get employee collection
    var collection = store.GetCollection("employee");
    
    var results = collection.AsQueryable().Where(e => e._id == (long)jObj["_id"]);
    if (!results.Any())
    {
        await collection.InsertOneAsync(jObj);
    }
    else
    {
        jObj = JObject.FromObject(results.First());
    }
    
    jObj["anArray"][1] = 14;  // modify the data
    
    await collection.UpdateOneAsync(e => e._id == 11, jObj); // pop up error dialog here
}

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions