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

C # operate MongoDB

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

1 install MongoDB.Driver

2 establish a database connection

# mongodb://127.0.0.1:27017 MongoClient client = new MongoClient ("connection string")

3 get the database

Var database = client.GetDatabase ("database")

4 get dataset Collection

# if the data is pre-defined, or if it is not defined, you can use the BsonDocument type, where BsonDocument represents a schema that is not scheduled. Var collection = database.GetCollection ("collection")

5 insert data

# InsertOne (synchronous insertion): collection.InsertOne (document); # InsertOneAsync (asynchronous insertion): await collection.InsertOneAsync (document); # if you want to insert multiple data, you can use the InsertMany or InsertManyAsync method.

6 query data

# find the first data in the collection var document = collection.Find (new BsonDocument ()). FirstOrDefault (); orvar filter = Builders.Filter.Empty;var document = collection.Find (filter). FirstOrDefault (); # query all data in the dataset var documents = collection.Find (new BsonDocument ()). ToList (); orvar filter = Builders.Filter.Empty;var document = collection.Find (filter). ToList (); # filter to get a single document var filterBuilder = Builders.Filter

7 Update the document

#. Update.Set () cooperates with filter modification var filter = Builders.Filter.Where (s = > s.Name = = "AESCR") & Builders.Filter.Where (d = > d.BooksArray.Any (x = > x.XXBooks.Any (y = > y.booknameplate = "netcore")); var update = Builders.Update.Set (d = > d.BooksArray [0] .XXBooks [0] .bookname, "NetCore"); collection.UpdateOne (filter, update)

8 query

Var result = collection.AsQueryable () .Where (s = > s.Name = = "AESCR") .SelectMany (s = > s.BooksArray) .Where (r = > r.bookname = = "C#") Var page = await _ sensorNodes.AsQueryable () / find the corresponding sensorNode .Where (s = > s.Id = = _ sensorNodeId) / Select the Records embedded array .SelectMany (s = > s.Records) / / sort by recording time .OrderBy (r = > r.RecorDateTime) / / Skip (index-1) * size) / / Select a page of data .Take (size) / / convert to a collection .ToListAsync ()

9 Delete

# Update.PullFilter () method delete filter delete element var update = Builders.Update.PullFilter (s = > s.Records, r = > r.Data = = 339119843.0); await _ sensorNodes.UpdateOneAsync (s = > s.Id = = _ sensorNodeId, update)

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