2021

MongoDB C#.NET Driver CRUD

A CRUD project using the official MongoDB C# driver to insert, query, and filter users in the Address_Book database.

MongoDB - C#.NET Driver

MongoDB - C#.NET Driver

This is a project I made to create a user into MongoDB under the database “Address_Book” and in “users” collection. You can Insert a new record
You can view All users in the collection
You can view all users from certain city and view certain user

0

The Class’s

● MongoDBCRUD

crud0

● Person

PersonClass

● Address

AddressClass

Insert Record

AddUser

The InsertRecord function is a generic function that contains the collection name users as “table” and a generic record which will return a new Person object.

InsertRecrodMethod

Now in Program I create a new instance of MongoDBCRUD and call it db(I could create it as a private instance but I do it as that for the example…),it should contain the Mongo database name(implemented inside MongoDBCRUD ctor) which is “Address_Book” and it should contain some object becasue the method demands some generic object so I give it a new instance of Person and give it all its neccessary via each text box text , created a new guid id([BsonId]) for the new user and a new Primary_Address instance which will have the Address info.

InsertRecrodButton

Load Records

viewUsers

The LoadRecords function will return a generic List and should contain the “table” name which is the users collection inside the database. I create a variable called collection which is using the private IMongoDatabase db; object and get the collection by the “table” name. Here I use the OptionBuilders which can help alot when deploying a query is neccessary. return the variable collection and find by the option builder variable to List.

LoadRecordsMethod

In Program I again create the db instance from MongoDBCRUD and create a new List which will contain < Person > called “persons”, and will be equal to db instance LoadRecords function and receive the generic value as < Person >.In the bracket I asked for the “table” name so i give it the “users” collection. To show all the users I tell the data grid view that its DataSource is the persons instance. When the “VIEW USERS” button is clicked ti will try to load all the records from the db into the DGV.

LoadRecordsButton

Load Records via City Name

ViewCity

The LoadRecordsByCity function will return A generic List and should contain the “table” name and an Address object. In the Builder I give it the Person class and start working on filtering. So I say that the variable filter is equal to Primary_Address.City which is given by the lambada expression and the value is the city object from Address. return the variable collection and find by the filter that I made.

LoadRecordsByCityMethod

In the Program Create db instance from MongoDBCRUD and give it the database name.Create A List using < Person > call it p and should be equal to LoadRecordsByCity function from db instance,receive the generic value as < Person > and say that table is “users” and where the Address object should be I create a new Address and say that the City is equal to the name that was written in the textbox to find all user by the city name.

LoadRecordsByCityButton

Load Records via User Name

ViewOneUser

The LoadRecordsByName function will return A generic List and should contain the “table” name and the first name of the user(Just for the demonstration). In the Builder I give it a generic value and say that its equal to “First_Nane” which means it will go to the collection and select what ever the string is and the value is “name”. return the variable collection and find by the filter that I made.

LoadRecordsByNameMethod

In Program Create db instance again Create A List using < Person > call it p and should be equal to LoadRecordsByName function from db instance,receive the generic value as < Person > and say that table is “users” and where the users first name is equal to the textbox text that was given by the user.

LoadRecordsByNameButton

Inside MongoDB Compass

Mongoshusers
Mongoshusers2

Screenshot

MongoDB CRUD Project
Explore more projects