public MongoStore(string db)
 {
     var clientSettings = new MongoClientSettings();
     //clientSettings.Server = new MongoServerAddress("mongodb://host:22666");
     var mongo = new MongoClient("mongodb://localhost:22666");
     var l = mongo.ListDatabasesAsync().Result;
     _mongodb = mongo.GetDatabase(db);
 }
        public async Task ListDatabasesAsync_should_invoke_the_correct_operation()
        {
            var operationExecutor = new MockOperationExecutor();
            var client = new MongoClient(operationExecutor);
            await client.ListDatabasesAsync();

            var call = operationExecutor.GetReadCall<IAsyncCursor<BsonDocument>>();

            call.Operation.Should().BeOfType<ListDatabasesOperation>();
        }
 /// <summary>
 ///     获得数据库列表
 /// </summary>
 /// <param name="client"></param>
 /// <returns></returns>
 public static List<BsonDocument> GetDatabaseList(MongoClient client)
 {
     //暂时返回一个同步的结果
     IAsyncCursor<BsonDocument> databaseCursor = null;
     var task = Task.Run(
         async () => { databaseCursor = await client.ListDatabasesAsync(); }
         );
     task.Wait();
     List<BsonDocument> databaseList = null;
     task = Task.Run(
         async () => { databaseList = await databaseCursor.ToListAsync(); }
         );
     task.Wait();
     return databaseList;
 }
        public void ListDatabases_should_invoke_the_correct_operation(
            [Values(false, true)] bool async)
        {
            var operationExecutor = new MockOperationExecutor();
            var client = new MongoClient(operationExecutor);

            if (async)
            {
                client.ListDatabasesAsync().GetAwaiter().GetResult();
            }
            else
            {
                client.ListDatabases();
            }

            var call = operationExecutor.GetReadCall<IAsyncCursor<BsonDocument>>();

            call.Operation.Should().BeOfType<ListDatabasesOperation>();
        }
Exemple #5
0
        private void GetDatabase(MongoSettings settings)
        {
            Require.NotNull(settings, "settings");

            // Register conventions.
            Register();

            // To directly connect to a single MongoDB server
            // (this will not auto-discover the primary even if it's a member of a replica set)
            var clientSettings = new MongoClientSettings
            {
                Server = new MongoServerAddress(settings.Host,settings.Port),
                ClusterConfigurator = builder =>
                {
                    builder.ConfigureCluster(s => s.With(serverSelectionTimeout: TimeSpan.FromSeconds(settings.Timeout)));
                }
            };
            var client = new MongoClient(clientSettings);

            this.Database = client.GetDatabase(settings.DatabaseName);

            try
            {
                var result = client.ListDatabasesAsync();
                result.Wait();
            }
            catch(AggregateException exception)
            {
                if (exception.InnerException is TimeoutException)
                    throw new UnreachableException(client, exception.InnerException as TimeoutException);
            }
            catch (Exception)
            {
                throw;
            }
        }
 /// <summary>
 ///     获得数据库定义列表
 /// </summary>
 /// <param name="client"></param>
 /// <returns></returns>
 public static List<BsonDocument> GetDatabaseInfoList(MongoClient client)
 {
     //暂时返回一个同步的结果
     IAsyncCursor<BsonDocument> databaseCursor = null;
     var task = Task.Run(
         async () => { databaseCursor = await client.ListDatabasesAsync(); }
         );
     task.Wait();
     List<BsonDocument> databaseList = null;
     task = Task.Run(
         async () => { databaseList = await databaseCursor.ToListAsync(); }
         );
     task.Wait();
     databaseList.Sort((x, y) =>
     {
         if (x.GetElement("name").Value.ToString() == ConstMgr.DatabaseNameAdmin)
         {
             return -1;
         }
         else
         {
             return x.GetElement("name").Value.ToString().CompareTo(y.GetElement("name").Value.ToString());
         }
     });
     return databaseList;
 }