受欢迎的博客标签

How to convert a convert json to documnet with the official MongoDB C# driver?

Published

 1.json to BsonDocument

string json = "{ 'foo' : 'bar' }";

MongoDB.Bson.BsonDocument document

= MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(json);

BsonDocument to json string.

BsonDocument.ToJson()

 

2.

var brdDoc = new BrdUser ();
brdDoc.UserNm = "invisible";
brdDoc.EmailAdrs = "[email protected]";
brdDoc.Questions = new []{ new Question{ Title = "Q", Desciption = "Question to ask" } };

var bsonDocument = brdDoc.ToBsonDocument ();
var jsonDocument = bsonDocument.ToJson ();

Console.WriteLine (jsonDocument);

output

{ 
   "_id" : null, 
   "username" : "invisible", 
   "email" : "[email protected]", 
   "Questions" : [{ "_id" : null, "title" : "Q", "desc" : "Question to ask" }] 
}

Convert mongodb Document to c# class

 var mongoclient = new MongoClient("mongodb://localhost:27017/netcore");
            var database = mongoclient.GetDatabase("netcore");
            var collection = database.GetCollection<BsonDocument>("BlogPost");

         
            var Alldocument = await collection.FindAsync(new BsonDocument());

            int count = 0;

            try
            {
                using (var cursor = Alldocument)
                {
                    while (await cursor.MoveNextAsync())
                    {

                        var batch = cursor.Current;

                        foreach (var document in batch)
                        {
                           

                            var blogPostModel = new BlogPostModel();
                            
                            blogPostModel.Id = document["_id"].ToString(); 

                            blogPostModel.Title = document["Title"].ToString(); 

                            string ContentStr = String.IsNullOrEmpty(document["Body"].ToString()) ? "aaaa" : document["Body"].ToString();

                            ContentStr = ContentStr + ".";

                            blogPostModel.Body = ContentStr;

                            blogPostModel.Tags = document["Tags"].ToString().ParseTags().ToList(); 

                            blogPostModel.CreatedOn = document["CreatedOnUtc"].ToUniversalTime();

                            blogPostModel.UpdatedOn = document["UpdatedOnUtc"].ToUniversalTime();
                                                 
                            blogPostModel.Slug = "http://wwwdemo.com/Blog/BlogPost/" + document["_id"].ToString()+"/"+ document["SeName"].ToString();

                            blogPostModel.IsDeleted = document["IsDeleted"].AsBoolean;

                       
                               model.BlogPosts.Add(blogPostModel);
                          
                            count++;
                       
                            if (count >= 100 && (count %100==0))
                            {
                                _logger.LogInformation("get data from Product {0} {1}",DateTime.Now.ToString() ,count.ToString());
                            }
                            
                        }





                    }
                }
            }
            catch
            {
                throw;
            }