public void Run(MongoDatabase database)
        {
            var collection = database.GetCollection("addresses");

            BsonClassMap.RegisterClassMap<Address>();

            var address = new Address();
            address.Line1 = "123 Main Street";
            address.Line2 = "";
            address.City = "New City";
            address.County = "Bergen";
            address.State = "New Jersey";

            // I feel like this is cheating.. Need to figure out how to actually serialize an object into a bson document, instead of
            // serializing into a byte array and then deserializing to a BsonDocument.
            var bson = address.ToBson();

            var doc = BsonSerializer.Deserialize<BsonDocument>(bson);
            
            var insert_task = collection.InsertOneAsync(doc);
            insert_task.Wait();

            Console.WriteLine("Inserted address");
            
            var count_task = collection.CountAsync(new BsonDocument()); // no filter

            count_task.Wait();
            var count = count_task.Result;

            Console.WriteLine("Count: " + count);
        }
示例#2
0
 public PersistToMongo(string connectionString)
 {
     _mongoServer = new MongoClient(connectionString).GetServer();
     var databaseName = MongoUrl.Create(connectionString).DatabaseName;
     _mongoDatabase = _mongoServer.GetDatabase(databaseName);
     _mongoCollection = _mongoDatabase.GetCollection(COLLECTION_NAME);
 }
示例#3
0
        public void Run(MongoDatabase database)
        {
            var collection = database.GetCollection("foobar");

            var temp = collection.CountAsync(new BsonDocument()); // no filter

            temp.Wait();
            var count = temp.Result;

            Console.WriteLine("Count: " + count);
        }
示例#4
0
        public void Run(MongoDatabase database)
        {
            var collection = database.GetCollection("foobar");

            var filter = Builders<BsonDocument>.Filter.Gt("foo", 9999);

            var temp = collection.CountAsync(filter);

            temp.Wait();
            var count = temp.Result;

            Console.WriteLine("Count: " + count);
        }
示例#5
0
 public SearchHashForm(string collectionName = "HashForm", string dbName = "emdsdb", string hostIP = "localhost")
 {
     connectionString = String.Format("mongodb://{0}/?safe=true", hostIP);
     try
     {
         server = MongoServer.Create(connectionString);
         database = server.GetDatabase(dbName);
         collection = database.GetCollection<FormHash>(collectionName);
         server.Ping();
     }
     catch
     {
         throw new Exception("Проблема подключением к монге");
     }
     log = FileLogger.GetLogger();
 }
示例#6
0
        public void Run(MongoDatabase database)
        {
            var collection = database.GetCollection("foobar");

            for (int i = 1; i < 100000; i++)
            {
                BsonDocument document = new BsonDocument
                {
                    { "foo", i }
                };

                var task = collection.InsertOneAsync(document);
                task.Wait();

                Console.WriteLine("Inserted: " + i);
            }
        }
示例#7
0
 public TrainLogRepository(string collectionName = "NeuralTrainLog", string dbName = "emdsdb", string connectionString = "mongodb://localhost/?safe=true")
 {
     CollectionName = collectionName;
     DbName = dbName;
     ConnectionString = String.Format("{0}/{1}", connectionString, dbName);
     try
     {
         var client = new MongoClient(ConnectionString);
         server = client.GetServer();
         database = server.GetDatabase(dbName);
         collection = database.GetCollection(collectionName);
         server.Ping();
     }
     catch
     {
         throw new Exception("Не удалось подключиться к MongoDB");
     }
 }
示例#8
0
 private TrainLogger(string collectionName = "NeuralTrainLog", string dbName = "emdsdb", string connectionStringMongo = "mongodb://localhost/?safe=true")
 {
     IdSession = Guid.NewGuid();
     isCreate = true;
     CollectionName = collectionName;
     DbName = dbName;
     AgeNumber = -1;
     connectionString = connectionStringMongo;
     try
     {
         server = MongoServer.Create(connectionString);
         database = server.GetDatabase(dbName);
         collection = database.GetCollection(collectionName);
         server.Ping();
     }
     catch
     {
         throw new Exception("Не удалось подключиться к MongoDB");
     }
 }
        public CreditCardRepository(string connection)
        {
            if (string.IsNullOrWhiteSpace(connection))
            {
                connection = "mongodb://localhost:27017";
            }

            _client = new MongoClient(connection);
            _server = _client.GetServer();
            _database = _server.GetDatabase("Customers");
            _creditCards = _database.GetCollection("CreditCard");

            // Reset database and add some default entries
            //_creditCards.RemoveAll();
            /*
            for (int index = 1; index < 5; index++)
            {
                CreditCardItem card = new CreditCardItem
                {
                    Number = string.Format("{0}{0}{0}", index)
                };
                AddCard(card);
            }*/
        }
示例#10
0
 public void TestDatabase()
 {
     MongoDatabase db = new MongoDatabase("localhost", "test_db");
     db.Connect();
     db.GetCollection("foo");
 }