0

Required to write an API which should accept a request body with any structure to convert to BsonDocument and save it. Something similar as below but not seems working with Object type.

[HttpPost("InsertBsonUserWithInput")]
        public async Task<ActionResult<Object>> InsertBsonUserWithInputObject(Object requestModel)
        {
            var dbClient = new MongoClient("mongodb://127.0.0.1:27017");
            IMongoDatabase db = dbClient.GetDatabase("Identity");
            var users = db.GetCollection<BsonDocument>("Users");
            users.InsertOne(requestModel.ToBsonDocument());
            return new OkObjectResult(requestModel);

        }

While trying so, the request object always comes as string or value type and unable to convert properly to BsonDocument. At the end any structure data will be able to receive by API and that required to convert to BsonDocument.

  • 1
    what error do you see? – dododo Jan 14 '23 at 13:08
  • MongoDB stores data as document (structure data). So this is correct behavior that only object (structure data) only can convert to BsonDocument. I think you should revise why you want to store the data from string, value type as document. – Yong Shun Jan 14 '23 at 14:03

0 Answers0