When I send {"name":"John Doe", "age":18, "country":"USA"} to my C# Web API with POST to api/test, I store it in my mongo test-collection and return the updated document:
[HttpPost]
[Route("{collection}")]
public IHttpActionResult Upsert(string collection, HttpRequestMessage request)
{
var document = request.Content.ReadAsStringAsync().Result;
var doc = BsonDocument.Parse(document);
var result = new Db(collection).Upsert(doc).Result;
return Ok(result);
}
.
public async Task<BsonDocument> Upsert(BsonDocument document)
{
if (document.Contains("_id"))
{
await _collection.ReplaceOneAsync(w => w["_id"] == document["_id"], document);
}
else
{
await _collection.InsertOneAsync(document);
}
return document;
}
This works, but the result is now a key-value object:
[
{
"_name": "_id",
"_value": "56e9364d942e1f287805e170"
},
{
"_name": "name",
"_value": "John Doe"
},
{
"_name": "age",
"_value": 18
},
{
"_name": "country",
"_value": "USA"
}
]
What I would expect is:
{
"_id": "56e9364d942e1f287805e170",
"name":"John Doe",
"age":18,
"country":"USA"
}
How can I achieve this?