当前位置: 首页>>代码示例>>C#>>正文


C# Driver.MongoClient类代码示例

本文整理汇总了C#中MongoDB.Driver.MongoClient的典型用法代码示例。如果您正苦于以下问题:C# MongoClient类的具体用法?C# MongoClient怎么用?C# MongoClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


MongoClient类属于MongoDB.Driver命名空间,在下文中一共展示了MongoClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetMongoContext

 public MongoDatabase GetMongoContext()
 {
     MongoClient client = new MongoClient(path);
     MongoServer server = client.GetServer();
     MongoDatabase dbContext = server.GetDatabase("mongotelerikchat");
     return dbContext;
 }
开发者ID:Varbanov,项目名称:TelerikAcademy,代码行数:7,代码来源:MongoChat.xaml.cs

示例2: IsRestored

    public bool IsRestored(string connectionName)
    {
      MongoServer server = null;
      var connectionString = ConfigurationManager.ConnectionStrings[connectionName]?.ConnectionString;

      if (string.IsNullOrEmpty(connectionString))
      {
        return false;
      }

      try
      {
        var mongoUrl = new MongoUrl(connectionString);
        var mongoClient = new MongoClient(mongoUrl);
        server = mongoClient.GetServer();

        return server.GetDatabase(mongoUrl.DatabaseName).CollectionExists(MongoRestoreSettings.RestoredDbTokenCollection);
      }
      catch (FormatException ex)
      {
        Log.Error("Wrong connection string format", ex, this);
        throw;
      }
      finally
      {
        server?.Disconnect();
      }
    }
开发者ID:kamsar,项目名称:Habitat,代码行数:28,代码来源:MongoRestoreService.cs

示例3: MongoContext

        public MongoContext()
        {
            var client = new MongoClient(ConfigurationManager.ConnectionStrings[DATABASE].ConnectionString);
            var server = client.GetServer();

            _database = server.GetDatabase(DATABASE);
        }
开发者ID:Beren1h,项目名称:Insight,代码行数:7,代码来源:MongoContext.cs

示例4: PatientRepository

 /// <summary />
 public PatientRepository()
 {
     var client = new MongoClient(ConfigurationManager.ConnectionStrings["Mongo_patientcare_datamock"].ConnectionString);
     _log = new Logger("WebAPI : PatientRepository");
     _db = client.GetDatabase("patientcare_datamock");
     _patients = _db.GetCollection<BsonDocument>("Patients");
 }
开发者ID:dsb92,项目名称:patientcare,代码行数:8,代码来源:PatientRepository.cs

示例5: MongoRepositoryContext

 static MongoRepositoryContext() {
     settings = new MongoRepositorySettings();
     var url = new MongoUrl(ConfigurationManager.ConnectionStrings["SysDB"].ConnectionString);
     client = new MongoClient(url);
     server = client.GetServer();
     database = server.GetDatabase(url.DatabaseName);
 }
开发者ID:wudan330260402,项目名称:Danwu.Core,代码行数:7,代码来源:MongoRepositoryContext.cs

示例6: GetMongoCollection

 private MongoCollection<BsonDocument> GetMongoCollection()
 {
     var client = new MongoClient(_connectionString);
     var db = client.GetServer().GetDatabase(new MongoUrl(_connectionString).DatabaseName);
     var collection = db.GetCollection(_collectionName);
     return collection;
 }
开发者ID:hxzqlh,项目名称:enode,代码行数:7,代码来源:MongoEventPublishInfoStore.cs

示例7: MainAsync

        static async Task MainAsync(string[] args)
        {
            var urlString = "mongodb://localhost:27017";
            var client = new MongoClient(urlString);
            var db = client.GetDatabase("students");
            var collection = db.GetCollection<BsonDocument>("grades");
            var filter = new BsonDocument("type","homework");
           // var count = 0;
            var sort = Builders<BsonDocument>.Sort.Ascending("student_id").Ascending("score");
            var result = await collection.Find(filter).Sort(sort).ToListAsync();
           var previous_id=-1 ;
           var student_id=-1;
            int count = 0;
            foreach (var doc in result)         
            {
                
                student_id = (int)doc["student_id"];
                    //Console.WriteLine(student_id);
                if (student_id != previous_id)
                {
                    count++;
                    previous_id = student_id;
                    Console.WriteLine("removing :{0} ", doc);
                   // await collection.DeleteOneAsync(doc);

                    await collection.DeleteManyAsync(doc);
                }



              // process document
            }
            Console.WriteLine(count);
            //Console.WriteLine(coll.FindAsync<"">);
        }
开发者ID:sahithrao153,项目名称:M101Dotnet,代码行数:35,代码来源:MongoDB.cs

示例8: MongoDbBase

 public MongoDbBase(string connectionString, string database)
 {
     this.connectionString = connectionString;
     this.database = database;
     Client = new MongoClient(connectionString);
     Database = Client.GetDatabase(database);
 }
开发者ID:Flavien,项目名称:mongodb-storage,代码行数:7,代码来源:MongoDbBase.cs

示例9: AddWord

        public async Task<ActionResult> AddWord()
        {
            var client = new MongoClient(_connection);
            var db = client.GetDatabase(_database);

            var words = db.GetCollection<BsonDocument>("Word");

            var word1 = new Word
            {
                Text = "Ape",
                Type = Enums.WordType.Noun.ToString()
            };

            var bsonObject = word1.ToBsonDocument();

            try
            {
                await words.InsertOneAsync(bsonObject);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }

            return Json("success", JsonRequestBehavior.AllowGet);
        }
开发者ID:kablitz,项目名称:SPRNT,代码行数:26,代码来源:NameGenController.cs

示例10: TodoItemRepository

 public TodoItemRepository(string mongoConnection, string databaseName)
 {
     collectionName = "todoitems";
     var client = new MongoClient(mongoConnection);
     this.database = client.GetDatabase(databaseName);
     this.collection = database.GetCollection<TodoItem>(collectionName);
 }
开发者ID:CedricLeblond,项目名称:MultiChannelTodo,代码行数:7,代码来源:TodoItemRepository.cs

示例11: MongoDbStore

 public static MongoDbWorkflowStore MongoDbStore()
 {
     var client = new MongoClient("mongodb://localhost");
     var server = client.GetServer();
     var database = server.GetDatabase("StatelessWorkflowTest");
     return new MongoDbWorkflowStore(database);
 }
开发者ID:rajeshgupthar,项目名称:Stateless.WorkflowEngine,代码行数:7,代码来源:BootStrapper.cs

示例12: MainAsync

        private async Task MainAsync()
        {
            var connStr = "mongodb://localhost:27017";
            var client = new MongoClient(connStr);
            var db = client.GetDatabase("store");
            var col = db.GetCollection<Product>("products");
            await db.DropCollectionAsync("products");

            Random random = new Random();

            List<string> descriptions = new List<string>() { "aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj" };
            List<string> categories = new List<string>() { "sports", "food", "clothing", "electronics" };
            List<string> brands = new List<string>() { "GE", "nike", "adidas", "toyota", "mitsubishi" };

            var docs = Enumerable.Range(0, 100)
                .Select(i => new Product
                {
                    Id = i,
                    SKU = String.Format("{0:D8}", i),
                    Price = random.NextDouble() * (100 - 1) + 1,
                    Description = descriptions[random.Next(descriptions.Count)],
                    Category = categories[random.Next(categories.Count)],
                    Brand = brands[random.Next(brands.Count)],
                    Reviews = new List<Review>(),
                });
            await col.InsertManyAsync(docs);

            await col.Indexes.CreateOneAsync(Builders<Product>.IndexKeys.Ascending(x => x.SKU), new CreateIndexOptions() { Unique = true });
            await col.Indexes.CreateOneAsync(Builders<Product>.IndexKeys.Descending(x => x.Price));
            await col.Indexes.CreateOneAsync(Builders<Product>.IndexKeys.Ascending(x => x.Description));
            await col.Indexes.CreateOneAsync(Builders<Product>.IndexKeys.Ascending(x => x.Category).Ascending(x => x.Brand));
            await col.Indexes.CreateOneAsync("{'reviews.author':1}");
        }
开发者ID:azmikamis,项目名称:M101NAssignments,代码行数:33,代码来源:Week4Assignment.cs

示例13: Main

        public static int Main(string[] args)
        {
            var client = new MongoClient("");
            var db = client.GetDatabase("guess-what");
            var templateCollection = db.GetCollection<BsonDocument>("templates");
            // Use this to delete single items or spam:

            // templateCollection.DeleteOne(new BsonDocument(new BsonElement("_id", BsonValue.Create("Zp-ifMYR_0W6D0GwKsuoSw"))));
            // templateCollection.DeleteOne(new BsonDocument(new BsonElement("_id", BsonValue.Create("Ss0ISYQbSEyUsYVnMjbDAw"))));

            /*
            var regex = new Regex("^<a href=\"http://.*#\\d*\">.*</a>,$");
            foreach (var template in templateCollection.Find(new BsonDocument()).ToList())
            {
                var description = template.GetElement("Description").Value.ToString().Trim();
                if (regex.Matches(description).Count > 0)
                {
                    templateCollection.DeleteOne(new BsonDocument (template.GetElement("_id")));
                }
                else
                {
                    Console.WriteLine("OK  :" + description);
                }
            }
            */
            Console.WriteLine("END.");
            Console.ReadLine();
            return 0;
        }
开发者ID:daniel-kun,项目名称:guess-what,代码行数:29,代码来源:Program.cs

示例14: InitMongoDB

        public static bool InitMongoDB(this Funq.Container container)
        {
            try
            {
                // Register the MySql Database Connection Factory
                var mongoDbConnectionString = ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString;
                var client = new MongoClient(mongoDbConnectionString);
                var server = client.GetServer();
                var database = server.GetDatabase("hello_world");
                container.Register<MongoDatabase>(c => database);

                BsonClassMap.RegisterClassMap<World>(cm => {
                    cm.MapProperty(c => c.id);
                    cm.MapProperty(c => c.randomNumber);
                });

                BsonClassMap.RegisterClassMap<Fortune>(cm => {
                    cm.MapProperty(c => c.id);
                    cm.MapProperty(c => c.message);
                });

                // Create needed tables in MySql Server if they do not exist
                return database.CreateWorldTable() && database.CreateFortuneTable();
            }
            catch
            {
                // Unregister failed database connection factory
                container.Register<MongoDatabase>(c => null);

                return false;
            }

        }
开发者ID:cmsimike,项目名称:FrameworkBenchmarks,代码行数:33,代码来源:AppHostConfigHelper.cs

示例15: GetDatabase

        public IMongoDatabase GetDatabase()
        {
            var client = new MongoClient(this.ConnectionString);
            var db = client.GetDatabase(this.DatabaseName);

            return db;
        }
开发者ID:ni4ka7a,项目名称:Molybdenum-Team,代码行数:7,代码来源:MongoProvider.cs


注:本文中的MongoDB.Driver.MongoClient类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。