草庐IT

IMongoDatabase

全部标签

c# - 如何模拟 IMongoDatabase

我正在使用Moq在ASP.NETCore项目中模拟我的对象。我想模拟以下IsConnection()方法:publicClient(IMongoClientclient){_client=client;}publicasyncTaskIsConectionOk(){varpingCommand=newBsonDocument("ping",1);varmongoDb=_client.GetDatabase("Name");varcommandResult=awaitmongoDb.RunCommandAsync(pingCommand);returncommandResult!=nul

c# - MongoDb.Driver IMongoDatabase.GetCollectionNamesAsync() 异常

我使用C#MongoDB.Driver库连接到MongoDB。一些有效的代码MongoClientclient=MongoClientBuilder.Build("77.199.99.90",27016,"admin","pwd");IReadOnlyListdbNames=this.client.GetDatabaseNamesAsync().Result;foreach(vardbnindbNames){IMongoDatabasemongoDb=client.GetDatabase(dbn);varcNames=mongoDb.GetCollectionNamesAsync().

c# - 如何使用 c# 在 mongo DB 驱动程序 2.0 版中使用 IMongoDatabase 接口(interface)实现 "FindOne"和 "EnsureIndex"?

当我使用FindOne时,它​​说“'MongoClientExtensions.GetServer(MongoClient)'isobsolete:'UsethenewAPIinstead.'Observer.Client”作为警告。这是我的代码collection.EnsureIndex(IndexKeys.Ascending("Username","Type"),IndexOptions.SetUnique(true));varuserlog=collection.FindOne(Query.Where(ul=>ul.Username==username&&ul.Type==ty