Advertisement
CryptoJones

C#MongoDB

Jul 11th, 2017
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MongoDB.Driver;
  7. using MongoDB.Bson;
  8.  
  9.  
  10.  
  11. namespace MongoDB_Test
  12. {
  13.     class Program
  14.     {
  15.         private static IMongoClient client;
  16.         private static IMongoDatabase database;
  17.  
  18.         protected static IMongoClient Client
  19.         {
  20.             get
  21.             {
  22.                 return client;
  23.             }
  24.  
  25.             set
  26.             {
  27.                 client = value;
  28.             }
  29.         }
  30.  
  31.         protected static IMongoDatabase Database
  32.         {
  33.             get
  34.             {
  35.                 return database;
  36.             }
  37.  
  38.             set
  39.             {
  40.                 database = value;
  41.             }
  42.         }
  43.  
  44.         static void Main(string[] args)
  45.         {
  46.  
  47.             Client = new MongoClient();
  48.             Database = Client.GetDatabase("test");
  49.  
  50.  
  51.          doJob();
  52.  
  53.         }
  54.  
  55. static void doJob()
  56.         {
  57.  
  58.             var document = new BsonDocument
  59. {
  60.     { "address" , new BsonDocument
  61.         {
  62.             { "street", "2 Avenue" },
  63.             { "zipcode", "10075" },
  64.             { "building", "1480" },
  65.             { "coord", new BsonArray { 73.9557413, 40.7720266 } }
  66.         }
  67.     },
  68.     { "borough", "Manhattan" },
  69.     { "cuisine", "Italian" },
  70.     { "grades", new BsonArray
  71.         {
  72.             new BsonDocument
  73.             {
  74.                 { "date", new DateTime(2014, 10, 1, 0, 0, 0, DateTimeKind.Utc) },
  75.                 { "grade", "A" },
  76.                 { "score", 11 }
  77.             },
  78.             new BsonDocument
  79.             {
  80.                 { "date", new DateTime(2014, 1, 6, 0, 0, 0, DateTimeKind.Utc) },
  81.                 { "grade", "B" },
  82.                 { "score", 17 }
  83.             }
  84.         }
  85.     },
  86.     { "name", "Vella" },
  87.     { "restaurant_id", "41704620" }
  88. };
  89.  
  90.             var collection = Database.GetCollection<BsonDocument>("restaurants");
  91.             try
  92.             {
  93.                 collection.InsertOne(document);
  94.             }
  95.             catch (Exception)
  96.             {
  97.  
  98.                 throw;
  99.             }
  100.            
  101.         }
  102.  
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement