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

What is the method of C # operating MongoDb insert, update and delete

2025-04-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

C# operation MongoDb insertion update and delete method is what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Next, we start to do insert, update and delete methods.

Demo is still using the previous one.

insert

Let's add a new button to the form and write the insert.

The insertion method is simple: instantiate a new one in the class, then assign a value to it, and then use collection.insert

Note: MongoServer server = new MongoClient (connectionStr). GetServer (); this method is out of date, so if we want to get MongoServer, we need to change it to

MongoClient client = new MongoClient (connectionStr)

MongoServer server = new MongoServer (MongoServerSettings.FromClientSettings (client.Settings))

The method is as follows

After execution, a new record with the name New name and age 15 will appear.

Let's re-click on the query above to see the results as follows

Update

There are two ways to update MongoDb, one is save, the other is update

Save is to find the record in the database, then update the entire document, and insert a new one if it doesn't exist.

There are many parameters in update, which need to be checked out first and then modified. Multiple records can be modified at the same time.

Update of Save method

We get information that is older than 21 years old, there are only two, one is 22 years old, the other is 38 years old, and then we increase it by 2 years, and the final result is that one is 24 years old and the other is 40 years old.

The Update method has not been studied yet.

Delete

The way to delete it is very simple. You can simply remove it in collection.

Collection.remove () requires input parameters

Collection.removeall () deletes all data

Parameters are obtained by means of Query.

In our example, we want to delete data that is less than 20 years old.

Var query = Query.LT ("age", 20)

Collection.Remove (query)

It can be realized.

If you want to delete from the class later, you can use the following masked method to delete the primary key in the database with the _ id in the class.

Var list = collection.AsQueryable () .Where (p = > p.age

< 20); list.ToList().ForEach(t =>

{

Var query = Query.EQ ("_ id", t._id)

Collection.Remove (query)

});

The effect is as follows: query all the data first.

Then we click the delete button and then click the query again.

The last name is the new name, and there are no more 15-year-old ones.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report