CryptoJones icon

C#MongoDB

CryptoJones | PRO | 07/12/17 02:32:28 AM UTC | 0 ⭐ | 926 👁️ | Never ⏰ | []
C# |

2.17 KB

|

None

|

0 👍

/

0 👎

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Bson;
 
 
 
namespace MongoDB_Test
{
    class Program
    {
        private static IMongoClient client;
        private static IMongoDatabase database;
 
        protected static IMongoClient Client
        {
            get
            {
                return client;
            }
 
            set
            {
                client = value;
            }
        }
 
        protected static IMongoDatabase Database
        {
            get
            {
                return database;
            }
 
            set
            {
                database = value;
            }
        }
 
        static void Main(string[] args)
        {
 
            Client = new MongoClient();
            Database = Client.GetDatabase("test");
 
 
         doJob();
 
        }
 
static void doJob()
        {
 
            var document = new BsonDocument
{
    { "address" , new BsonDocument
        {
            { "street", "2 Avenue" },
            { "zipcode", "10075" },
            { "building", "1480" },
            { "coord", new BsonArray { 73.9557413, 40.7720266 } }
        }
    },
    { "borough", "Manhattan" },
    { "cuisine", "Italian" },
    { "grades", new BsonArray
        {
            new BsonDocument
            {
                { "date", new DateTime(2014, 10, 1, 0, 0, 0, DateTimeKind.Utc) },
                { "grade", "A" },
                { "score", 11 }
            },
            new BsonDocument
            {
                { "date", new DateTime(2014, 1, 6, 0, 0, 0, DateTimeKind.Utc) },
                { "grade", "B" },
                { "score", 17 }
            }
        }
    },
    { "name", "Vella" },
    { "restaurant_id", "41704620" }
};
 
            var collection = Database.GetCollection<BsonDocument>("restaurants");
            try
            {
                collection.InsertOne(document);
            }
            catch (Exception)
            {
 
                throw;
            }
            
        }
 
    }
 
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎