0

I have a .Net Core 2.0 Web API in which I'm using the MongoDB .net driver.

I have a class called ListOfValues:

public class ListOfValues
{
    public string ListName { get; set; }
    public List<object> MetaData { get; set; }
}

My controller's endpoint for saving is:

public class LovController : Controller
{
    [HttpPost]
    public async Task<IActionResult> Create([FromBody] ListOfValues lov)
    {
        if (!ModelState.IsValid)
            return BadRequest();

        await ldbContext.GetCollection<ListOfValues>("ListOfValues").InsertOneAsync(lov);
        return CreatedAtAction("Create", new { Id = lov.Id });
    }
}

I'm posting this data:

{
    "listName": "test",
    "MetaData": [{
        "Name": "Text",
        "FieldType": "TextBox",
        "isActive": 1
    }]
}

For some reason, when I query the DB I see this value:

{
    "_id" : ObjectId("5a1421e644d7cb07a8d3c45d"),
    "ListName" : "test",
    "MetaData" : [ 
        {
            "_t" : "Newtonsoft.Json.Linq.JObject, Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed",
            "_v" : {
                "Name" : {
                    "_t" : "JValue",
                    "_v" : []
                },
                "FieldType" : {
                    "_t" : "JValue",
                    "_v" : []
                },
                "isActive" : {
                    "_t" : "JValue",
                    "_v" : []
                }
            }
        }
    ]
}

I know that if I use a list of a specified type such as class or struct it will work, but I need the list to be a dynamic object since I don't know which objects the client may send.

What do I need to do for the data saved in the DB to be readable?

4
  • Can you please add examples of your metadata classes in C#? Your issue is related to Newtonsoft.Json; it has nothing to do with MongoDB driver. Commented Nov 21, 2017 at 14:27
  • I don't have classes for the metadata property. It's supposed to be a dynamic JSON array that the client sends. I don't even have a reference to Newtonsoft.Json in my code so I don't know why it appears there. Commented Nov 21, 2017 at 14:28
  • Oh I see. So, you would like data in the DB to look the same as in the request, right? Commented Nov 21, 2017 at 14:30
  • Yes, otherwise I cannot pull it back after that. Commented Nov 21, 2017 at 14:31

1 Answer 1

2

Use Dictionary<string, object> to represent data of unknown structure.

This works both with Newtonsoft.Json, which is the default serializer in ASP.NET Web API, and with BSON serializers of MongoDB.Driver.

Change the ListOfValues class as follows:

public class ListOfValues
{
    [JsonIgnore]
    public ObjectId Id { get; set; }

    public string ListName { get; set; }

    public List<Dictionary<string,object>> MetaData { get; set; }
}

The Id property is required in order to fetch data from the DB. With JsonIgnore attribute, it won't be present in the JSON returned/received by the controller.

Tested with:

  • ASP.NET Core 2.0 Web API
  • Newtonsoft.Json 10.0.3
  • MongoDB.Driver 2.4.4
  • MongoDB server 3.4 w/WiredTiger
Sign up to request clarification or add additional context in comments.

5 Comments

It's so simple and obvious I don't know how I missed it. But, I've also found a very useful link in the MongoDB manual that covers a lot of interesting materials to know about working with the MongoDB C# driver - Serialize Documents with the CSharp Driver
For me, using Dictionary<string,object> only avoided using "_t" and "_v" for the top-level values. For nested values, it still uses _t and _v. Any ideas?
@Meglio any luck making it work with nested values?
@hahaha unfortunately no
@Meglio if you are still interested for a solution, this helped me out stackoverflow.com/a/47515074/3522714

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.