Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use MongoDB and basic CRUD operations in .NET

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to use MongoDB and basic CRUD operations in .NET, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to know it.

MongoDB

NoSQL is a document-oriented database system that is stored in a specific format, such as JSON. MongoDB is one of the most popular NoSQ database products. It has the characteristics of high performance, easy to deploy, easy to use, and rich lightweight binary format. The most important feature of MongoDB is that the query language supported is very powerful, which can almost achieve most of the functions of single table query in relational database, and it also supports indexing of data. it is a collection-oriented and schema-free document database.

This article will introduce in detail the installation of MongoDB, the use of MongoDB (C # code) in .NET applications, and demonstrate basic CRUD operations.

Advantages of MongoDB

It is convenient to store a large amount of data with simple structure

High speed, suitable for large and complex data environment

High flexibility and availability on the cloud

It is easy to expand

Using the dynamic mode of MongoDB, rapid development can be achieved.

MongoDB does not save data in rows, and all data is stored in a single document. From the programmer's point of view, MongoDB can handle pure JSON files. For more information, please see the following example of MongoDB document:

{"employeeid": ObjectId ("R8rGt541b1ad7d0c4rcfgt7549"), "employeename": "Tapas Pal", "department": "IT", "numberOfArticles": 50, "Address": ["street": "Some Street", "city": "Kolkata", "country": "India"], "companyname": "XYZ", "skills": [".NET", "JavaScript" "NoSQL", "Node.js", "HTML"]} get MongoDB

MongoDB is a free open source database that you can download from specific links. MongoDB also provides a large list of drivers to facilitate communication between different programming languages and databases. If you want to use it with .NET, you need to install the driver from the NuGet package. NET drivers include MongoDB.Bson,MongoDB.Driver and MongoDB.Driver.Core.

.net project

Now, let's create a .NET console application to demonstrate CRUD operations on MongoDB. When you run mongod.exe, make sure that the prompt command displays properly so that you can keep the MongoDB server running when you execute the console application.

Step 1

Open Visual Studio and create a C# console application project. Name it MongoDBConsoleApp.

Figure 1: new console application

Step 2

Install the MongoDB.NET driver. Right-click on the stand-alone solution, enter "Manage NuGet Packages.", and then type "MongoDB" in the search bar to display the installation package to install. See the following figure for details:

Figure 2: "Manage NuGet Packages" link

Figure 3: package Manager output

Step 3

Add the following namespace to the Program.cs file, which is used to connect MongoDB and CRUD operations.

Using MongoDB.Driver;using MongoDB.Bson; step 4

Next, write the code to the CRUD operation. In this example, you will use BsonDocument class objects to store data in the MongoDB database.

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using MongoDB.Driver;using MongoDB.Bson;namespace MongoDBConsoleApp {class Program {static void Main (string [] args) {try {MongoClient MongodbClient = new MongoClient ("mongodb://127.0.0.1:27017") / / GetDatabase and Collection IMongoDatabase Mongodb = MongodbClient.GetDatabase ("MyDatabase"); var MongodbcollList = Mongodb.ListCollections () .ToList (); Console.WriteLine ("The MongoDB list of collections are:"); foreach (var item in MongodbcollList) {Console.WriteLine (item) } var mydocument = Mongodb.GetCollection ("mydocument"); create (Mongodb, mydocument); update (Mongodb, mydocument); delete (Mongodb, mydocument); var myresultDoc = mydocument.Find (new BsonDocument ()). ToList () Foreach (var myitem in myresultDoc) {Console.WriteLine (myitem.ToString ());}} catch (Exception ex) {Console.WriteLine (ex.Message);} Console.ReadLine () } static void create (IMongoDatabase Mongodb, var mydocument) {BsonElement employeename = new BsonElement ("employeename", "Tapas Pal"); BsonDocument empployee = new BsonDocument (); empployee.Add (employeename); empployee.Add (new BsonElement ("employeenumber", 123); mydocument.InsertOne (empployee) } static void update (IMongoDatabase Mongodb, var mydocument) {/ / UPDATE BsonElement updateemployee = new BsonElement ("employeename", "Tapas1 Pal1"); BsonDocument updateemployeedoc = new BsonDocument (); updateemployeedoc.Add (updateemployee); updateemployeedoc.Add (new BsonElement ("employeenumber", 1234)) BsonDocument findemployeeDoc = new BsonDocument (new BsonElement ("employeename", "Tapas Pal")); var updateDoc = mydocument.FindOneAndReplace (findemployeeDoc, updateemployeedoc); Console.WriteLine (updateDoc);} static void delete (IMongoDatabase Mongodb, var mydocument) {/ / DELETE BsonDocument findAnotheremployee = new BsonDocument (new BsonElement ("employeename", "Tapas1 Pal1")) Mydocument.FindOneAndDelete (findAnotheremployee);}

In the previous code method, the author retrieves the current database collection and then creates a BsonDocument object to store employee data.

In the create method, the author shows how to create a BsonElement object variable to store the key values of the employee object, and then add the object to the BsonDocument.

In the update method, the author passes the object directly as a parameter and finds the employee name to update the details.

In the delete method, the author deletes the employee that was previously created.

Finally, the entire list is displayed.

Thank you for reading this article carefully. I hope the article "how to use MongoDB and basic CRUD operations in .NET" shared by the editor will be helpful to you. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report