Reputation: 429
I am trying to insert data in mongodb by using BsonDocument. it inserting the data like following .
{
"_id" : ObjectId("5bf3eae0118cd3f6140aee72"),
"_t" : "MongoDB.Bson.BsonDocument, MongoDB.Bson",
"_v" : {
"_t" : "Newtonsoft.Json.Linq.JObject, Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed",
"_v" : {
"Email" : {
"_t" : "JValue",
"_v" : []
},
"Password" : {
"_t" : "JValue",
"_v" : []
}
}
}
}
The bellow code is i am using
public void Post([FromBody] Object value)
{
var document = new BsonDocument ();
document.AddRange(value.ToBsonDocument());
_database.GetCollection<dynamic>("Registeration").InsertOneAsync(document);
//Console.WriteLine("Success") ;
}
In this i am using .net core api without model class.How can i insert the data in correct way. Please any one try to help me.
Thank you...
Upvotes: 0
Views: 1904
Reputation: 429
I am changing bellow code
var document = new BsonDocument ();
document.AddRange(value.ToBsonDocument());
_database.GetCollection<dynamic>("Registeration").InsertOneAsync(document);
to
var obj = BsonDocument.Parse(value.ToString());
_database.GetCollection<dynamic>("Registeration").InsertOne(obj);
OutPut:
"_id" : ObjectId("5bf3f54b118cd3f6140aefe4"),
"_t" : "MongoDB.Bson.BsonDocument, MongoDB.Bson",
"_v" : {
"Email" : "test",
"Password" : "test"
}
Upvotes: 1