This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Newtonsoft.Json Dependency required | |
| JObject o = new JObject | |
| { | |
| { "name1", "value1" }, | |
| { "name2", "value2" } | |
| }; | |
| //Method-1 | |
| foreach (JProperty property in o.Properties()) | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| var database1 = clientDB.GetDatabase(dbName); | |
| var collection1 = database1.GetCollection<BsonDocument>(collectionName); | |
| //method1 | |
| using (var cursor = await collection1.Find(new BsonDocument()).ToCursorAsync()) | |
| { | |
| while(await cursor.MoveNextAsync()) | |
| { | |
| foreach (var doc in cursor.Current) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Serialization to JSON in C# | |
| //Newtonsoft.Json library require | |
| using Newtonsoft.Json; | |
| namespace .... | |
| { | |
| class Movie | |
| { | |
| public int Id {get; set} //Property |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //If you apply this code on whole BSON file(Entire Documents in single collection) then it couldn't works, so you need to take all the BSON document | |
| //in the List and by using foreach statment you need to extract each document and then convert into JSON. | |
| //example: ... | |
| // var database = clientApp.GetDb(dbName); | |
| // var collection = database.GetCollection<BsonDocument>(collectionName); | |
| // var documents = collection.Find(new BsonDocument()).ToList(); | |
| // foreach (BsonDocument doc in documents) | |
| // { | |
| // var jsonWritersetting = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; | |
| // JObject json = JObject.Parse(doc.ToJson<MongoDB.Bson.BsonDocument>(jsonWritersetting)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static void Main (string[] args) | |
| { | |
| var connectionString = "<Connection String>"; //example: ... = "mongodb://localhost"; | |
| var client = new MongoClient(connectionString); | |
| var database = client.GetDatabase("<Database Name>"); //example: ...GetDatabase("test"); | |
| string text = System.IO.File.ReadAllText(@"<File Directory with FileName.json>"); //example: ...ReadAllText(@"MyMovies.json"); | |
| var document = BsonSerializer.Deserialize<BsonDocument>(text); |