示例#1
0
        public async Task IndexDocumentsAsync <T>(List <T> documents) where T : class
        {
            MongoClient    client   = new MongoClient(_mongoDbConfiguration.ConnectionString);
            IMongoDatabase database = client.GetDatabase(_mongoDbConfiguration.DatabaseName);

            await database.GetCollection <T>(CollectionDiscover.GetCollection <T>()).InsertManyAsync(documents);
        }
        public async Task <T> GetSingleDocumentAsync <T>() where T : class
        {
            MongoClient    client   = new MongoClient(_mongoDbConfiguration.ConnectionString);
            IMongoDatabase database = client.GetDatabase(_mongoDbConfiguration.DatabaseName);
            T document = await(await database.GetCollection <T>(CollectionDiscover.GetCollection <T>()).FindAsync(new BsonDocument())).FirstOrDefaultAsync();

            return(document);
        }
示例#3
0
        public async Task ReplaceDocumentAsync <T>(T document, Expression <Func <T, bool> > match) where T : class
        {
            MongoClient    client   = new MongoClient(_mongoDbConfiguration.ConnectionString);
            IMongoDatabase database = client.GetDatabase(_mongoDbConfiguration.DatabaseName);

            ReplaceOneResult replaceOneResult = await database.GetCollection <T>(CollectionDiscover.GetCollection <T>()).ReplaceOneAsync(match, document);

            if (replaceOneResult.MatchedCount == 0)
            {
                await IndexDocumentAsync(document);
            }
        }
示例#4
0
 public async Task WipeCollectionAsync <T>() where T : class
 {
     MongoClient    client   = new MongoClient(_mongoDbConfiguration.ConnectionString);
     IMongoDatabase database = client.GetDatabase(_mongoDbConfiguration.DatabaseName);
     await database.GetCollection <T>(CollectionDiscover.GetCollection <T>()).DeleteManyAsync(arg => true);
 }