RocketMongo is a utility library designed to simplify interactions with MongoDB. This library provides methods to connect to MongoDB, manage databases and collections, and perform common database operations asynchronously.
Install the RocketMongo package via NuGet Package Manager Console:
Install-Package SaeedSafi.RocketMongo
this is real example usage of this package connect to mongo
var DB = await RocketMongo.ConnectMongoAndGetDatabase("mongodb://127.0.0.1:27017","Test")
do what ever you want. now have access on all of method you need
Connects to a MongoDB server and retrieves the specified database.
csharp
Copy code
public static IMongoDatabase ConnectMongoAndGetDatabase(string Config, string DbName)
Config
(string): The connection string for the MongoDB server.DbName
(string): The name of the database to retrieve.
IMongoDatabase: The specified database from the MongoDB server, or null if an error occurs.
Checks if a collection exists in the specified database asynchronously.
csharp
Copy code
public async static Task<bool> IsCollectionExistAsync(this IMongoDatabase Database, string CollectionName)
Database
(IMongoDatabase): The database to check for the collection.CollectionName
(string): The name of the collection to check for.
Task: A task representing the asynchronous operation that returns true if the collection exists, otherwise false.
Creates multiple collections in the specified database asynchronously.
public static async Task<bool> CreateManyCollectionsAsync(this IMongoDatabase Database, List<string> CollectionNames)
Database
(IMongoDatabase): The database in which to create the collections.CollectionNames
(List): A list of names of the collections to create.
Task: A task representing the asynchronous operation that returns true if all collections are created successfully, otherwise false.
Creates a single collection in the specified database asynchronously.
public async static Task<bool> CreateOneCollectionAsync(this IMongoDatabase Database, string CollectionName)
Database
(IMongoDatabase): The database in which to create the collection.CollectionName
(string): The name of the collection to create.
Task: A task representing the asynchronous operation that returns true if the collection is created successfully, otherwise false.
Connects to a MongoDB server.
public static IMongoClient ConnectMongo(string Config)
Config
(string): The connection string for the MongoDB server.
IMongoClient: The MongoDB client object, or null if an error occurs.
Creates a new database on the MongoDB server.
public static bool CreateOneDatabase(this IMongoClient client, string name)
client
(IMongoClient): The MongoDB client.name
(string): The name of the database to create.
bool: True if the database is created successfully, otherwise false.
Creates a collection in the specified database asynchronously.
public static async Task<bool> CreateCollectionAsync(this IMongoDatabase Database, string name)
Database
(IMongoDatabase): The database in which to create the collection.name
(string): The name of the collection to create.
Task: A task representing the asynchronous operation that returns true if the collection is created successfully.
Retrieves a database by its name from the MongoDB client.
public static IMongoDatabase GetDatabaseByName(this MongoClient Client, string DatabaseName)
Client
(MongoClient): The MongoDB client.DatabaseName
(string): The name of the database to retrieve.
IMongoDatabase: The specified database.
Retrieves a collection from the specified database.
public static IMongoCollection<TEntity> GetCollectionByName<TEntity>(this IMongoDatabase db, string CollectionName)
db
(IMongoDatabase): The database from which to retrieve the collection.CollectionName
(string): The name of the collection to retrieve.
IMongoCollection: The specified collection.
Makes a collection queryable.
public static IMongoQueryable<TEntity> MakeCollectionAsQueryAble<TEntity>(this IMongoCollection<TEntity> collection)
collection
(IMongoCollection): The collection to make queryable.
IMongoQueryable: A queryable version of the collection.
Retrieves all data from a collection asynchronously, optionally filtering with an expression.
public static async Task<List<TEntity>> GetAsync<TEntity>(this IMongoCollection<TEntity> collection, Expression<Func<TEntity, bool>>? expression = null)
collection
(IMongoCollection): The collection to query.expression
(Expression<Func<TEntity, bool>>?): An optional filter expression.
Task<List>: A task representing the asynchronous operation that returns a list of entities from the collection.
Inserts a single document into the specified collection asynchronously.
public static async Task<bool> InsertOneToCollectionAsync<TEntity>(this IMongoCollection<TEntity> Collection, TEntity Model)
Collection
(IMongoCollection): The collection into which to insert the document.Model
(TEntity): The document to insert.
Task: A task representing the asynchronous operation that returns true if the document is inserted successfully, otherwise false.
Inserts multiple documents into the specified collection asynchronously.
public static async Task<bool> InsertManyToCollectionAsync<TEntity>(this IMongoCollection<TEntity> Collection, List<TEntity> ModelList)
Collection
(IMongoCollection): The collection into which to insert the documents.ModelList
(List): The list of documents to insert.
Task: A task representing the asynchronous operation that returns true if the documents are inserted successfully, otherwise false.
Deletes a single document from the specified collection asynchronously based on a filter expression.
public static async Task<bool> DeleteOneFromCollectionAsync<TEntity>(this IMongoCollection<TEntity> Collection, Expression<Func<TEntity, bool>> expression)
Collection
(IMongoCollection): The collection from which to delete the document.expression
(Expression<Func<TEntity, bool>>): The filter expression to identify the document to delete.
Task: A task representing the asynchronous operation that returns true if the document is deleted successfully, otherwise false.
Deletes multiple documents from the specified collection asynchronously based on a filter expression.
public static async Task<bool> DeleteManyFromCollectionAsync<TEntity>(this IMongoCollection<TEntity> Collection, Expression<Func<TEntity, bool>> expression)
Collection
(IMongoCollection): The collection from which to delete the documents.expression
(Expression<Func<TEntity, bool>>): The filter expression to identify the documents to delete.