Skip to content

Instantly share code, notes, and snippets.

@fasetto
Created October 16, 2017 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fasetto/765aaea4a672ae78e381c6a51e339a0b to your computer and use it in GitHub Desktop.
Save fasetto/765aaea4a672ae78e381c6a51e339a0b to your computer and use it in GitHub Desktop.
public virtual async Task<IEnumerable<TEntity>> GetByFieldAsync(string field, string value)
{
var filter = Builders<TEntity>.Filter.Eq(field, value);
var result = await Collection.Find(filter).ToListAsync();
return result;
}
public virtual async Task<TEntity> GetByIdAsync(ObjectId id)
{
var filter = Builders<TEntity>.Filter.Eq("_id", id);
var entity = await Collection.Find(filter).FirstOrDefaultAsync();
return entity;
}
public virtual async Task<long> DeleteAllAsync()
{
var filter = new BsonDocument();
var result = await Collection.DeleteManyAsync(filter);
return result.DeletedCount;
}
public virtual async Task<bool> UpdateAsync(ObjectId id, string field, string value)
{
var update = Builders<TEntity>.Update.Set(field, value);
var result = await Collection.UpdateOneAsync(u => u.Id == id, update);
return result.ModifiedCount != 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment