0

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

4
  • 1
    That's not how data is meant to be stored. That looks like the serialization of a complex object rather than "raw data". Where is value actually coming from when fed into the function? The malformed fields would indicate that you are attempting to parse JSON. So your question really should be "How to marshal JSON into a BsonDocument". That's a clear title. Basically you need to show how you are reading the JSON as well. That's the real problem. Commented Nov 20, 2018 at 11:09
  • Also, please don't post screenshots. You can switch your GUI to a view which shows the "text" representation of the object structure, or simply use the mongo shell as I would recommend for questions posted here. Pictures just don't translate well and was in fact totally missed by the first few people viewing your post. Commented Nov 20, 2018 at 11:16
  • I am using postman to send the value Commented Nov 20, 2018 at 11:16
  • We need your code that is reading the JSON. See How to create a Minimal, Complete, and Verifiable example at it's not about the "insert" but rather the way you are retrieving the data before this code currently in the question ever gets called. Please edit your question with details as outlined there. Commented Nov 20, 2018 at 11:19

1 Answer 1

1

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"
    }
Sign up to request clarification or add additional context in comments.

Comments

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.