本文整理汇总了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;
}
示例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();
}
}
示例3: MongoContext
public MongoContext()
{
var client = new MongoClient(ConfigurationManager.ConnectionStrings[DATABASE].ConnectionString);
var server = client.GetServer();
_database = server.GetDatabase(DATABASE);
}
示例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");
}
示例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);
}
示例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;
}
示例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<"">);
}
示例8: MongoDbBase
public MongoDbBase(string connectionString, string database)
{
this.connectionString = connectionString;
this.database = database;
Client = new MongoClient(connectionString);
Database = Client.GetDatabase(database);
}
示例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);
}
示例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);
}
示例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);
}
示例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}");
}
示例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;
}
示例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;
}
}
示例15: GetDatabase
public IMongoDatabase GetDatabase()
{
var client = new MongoClient(this.ConnectionString);
var db = client.GetDatabase(this.DatabaseName);
return db;
}