Created
April 4, 2021 23:43
-
-
Save AleksaDjordjic/4533d89e0e931acee35ee8cf181523b7 to your computer and use it in GitHub Desktop.
A simple MogoDB C# client for basic CRUD operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using MongoDB.Bson; | |
using MongoDB.Driver; | |
using Newtonsoft.Json; | |
using System.Collections.Generic; | |
using MongoDB.Bson.Serialization.Attributes; | |
namespace MongoDBClient | |
{ | |
public abstract class MongoObject | |
{ | |
[BsonId] | |
[JsonConverter(typeof(ObjectIdConverter))] | |
public ObjectId ID { get; set; } | |
} | |
public class Mongo | |
{ | |
private IMongoDatabase database; | |
public Mongo(string database) | |
{ | |
var client = new MongoClient(); | |
this.database = client.GetDatabase(database); | |
} | |
public Mongo(string database, string host) : this(database, host, 27017) { } | |
public Mongo(string database, string host, ushort port) | |
{ | |
var client = new MongoClient($"mongodb://{host}:{port}"); | |
this.database = client.GetDatabase(database); | |
} | |
public ObjectId InsertObject(string collectionName, MongoObject obj) | |
{ | |
var collection = database.GetCollection<MongoObject>(collectionName); | |
collection.InsertOne(obj); | |
return obj.ID; | |
} | |
public List<T> LoadObjects<T>(string collectionName) | |
{ | |
var collection = database.GetCollection<T>(collectionName); | |
return collection.Find(new BsonDocument()).ToList(); | |
} | |
public List<T> LoadLastAmountObjects<T>(string collectionName, int amount) | |
{ | |
var collection = database.GetCollection<T>(collectionName); | |
return collection.Find(new BsonDocument()).Limit(amount).ToList(); | |
} | |
public bool LoadObjectById<T>(string collectionName, ObjectId id, out T result) | |
{ | |
var collection = database.GetCollection<T>(collectionName); | |
var filter = Builders<T>.Filter.Eq("ID", id); | |
result = collection.Find(filter).FirstOrDefault(); | |
return result != null; | |
} | |
public bool LoadObjectByProperty<T>(string collectionName, string property, object value, out T result) | |
{ | |
var collection = database.GetCollection<T>(collectionName); | |
var filter = Builders<T>.Filter.Eq(property, value); | |
result = collection.Find(filter).FirstOrDefault(); | |
return result != null; | |
} | |
public List<T> LoadObjectsByProperty<T>(string collectionName, string property, object value) | |
{ | |
var collection = database.GetCollection<T>(collectionName); | |
var filter = Builders<T>.Filter.Eq(property, value); | |
return collection.Find(filter).ToList(); | |
} | |
public List<T> LoadObjectsByFilter<T>(string collectionName, FilterDefinition<T> filter) | |
{ | |
var collection = database.GetCollection<T>(collectionName); | |
return collection.Find(filter).ToList(); | |
} | |
public bool LoadObjectByFilter<T>(string collectionName, FilterDefinition<T> filter, out T result) | |
{ | |
var collection = database.GetCollection<T>(collectionName); | |
result = collection.Find(filter).FirstOrDefault(); | |
return result != null; | |
} | |
public bool UpsertObject(string collectionName, MongoObject obj) | |
{ | |
var collection = database.GetCollection<MongoObject>(collectionName); | |
return collection.ReplaceOne( | |
new BsonDocument("_id", obj.ID), obj, new ReplaceOptions { IsUpsert = true }) | |
.IsAcknowledged; | |
} | |
public bool UpsertObjectById(string collectionName, ObjectId id, MongoObject obj) | |
{ | |
var collection = database.GetCollection<MongoObject>(collectionName); | |
obj.ID = id; | |
return collection.ReplaceOne( | |
new BsonDocument("_id", id), obj, new ReplaceOptions { IsUpsert = true }) | |
.IsAcknowledged; | |
} | |
public long CountCollection(string colletionName) | |
{ | |
var collection = database.GetCollection<MongoObject>(colletionName); | |
return collection.CountDocuments(new BsonDocument()); | |
} | |
public long CountCollectionByProperty<T>(string collectionName, string property, object value) | |
{ | |
var collection = database.GetCollection<T>(collectionName); | |
var filter = Builders<T>.Filter.Eq(property, value); | |
return collection.CountDocuments(filter); | |
} | |
public long CountCollectionByFilter<T>(string collectionName, FilterDefinition<T> filter) | |
{ | |
var collection = database.GetCollection<T>(collectionName); | |
return collection.CountDocuments(filter); | |
} | |
public bool DeleteObject<T>(string collectionName, ObjectId id) | |
{ | |
var collection = database.GetCollection<T>(collectionName); | |
var filter = Builders<T>.Filter.Eq("ID", id); | |
return collection.DeleteOne(filter).IsAcknowledged; | |
} | |
public bool DeleteObjectByProperty<T>(string collectionName, string property, object value) | |
{ | |
var collection = database.GetCollection<T>(collectionName); | |
var filter = Builders<T>.Filter.Eq(property, value); | |
return collection.DeleteOne(filter).IsAcknowledged; | |
} | |
public bool DeleteObjectByFilter<T>(string collectionName, FilterDefinition<T> filter) | |
{ | |
var collection = database.GetCollection<T>(collectionName); | |
return collection.DeleteOne(filter).IsAcknowledged; | |
} | |
public bool CollectionExists<T>(string collectionName) | |
{ | |
var collection = database.GetCollection<T>(collectionName); | |
return collection != null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment