简体   繁体   中英

How to insert data in mongodb in .net core2.1

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...

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"
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM