1024programmer Java MongoDB add, delete, modify

MongoDB add, delete, modify

Introduction to MongoDB driver types 1. MongoDB official driver: mongo-csharp-driver, download address: https://github.com/mongodb/mongo-csharp-driver/downloads 2. Third-party driver samus , this is a frequently used driver with a relatively fast update frequency. In addition to supporting general operations, the samus driver also supports Linq

Introduction to MongoDB driver types

1. MongoDB official driver: mongo-csharp-driver, download address: https://github.com/mongodb/mongo-csharp-driver/downloads

2. The third-party driver samus is a commonly used driver with a relatively fast update frequency. In addition to supporting general operations, the samus driver also supports Linq and Lambda expressions. Download address: https://github.com/samus/mongodb-csharp.
The operation process of the two mongodb drivers on the mongodb database is basically the same, but there are differences in the implementation methods. For example, the samus driver not only supports general formal operations, but also supports Linq and Lambda expressions.

Use MongoDB official driver to operate the database

Unzip the package and get the following two files:
MongoDB.Bson.dll: Serialization, Json related

MongoDB.Driver.dll: mongodb driver

Add references and introduce the above two DLLs into the project

Introducing namespaces into code

 using MongoDB.Bson;
 using MongoDB.Driver;

Obtain database connection service

 string cOnnectionString= " mongodb://localhost " ; //mongodb://[username:password@]hostname[:port][/[database][?options]]
 MongoServer server = MongoServer.Create(connectionString); // Connect to a MongoServer

Get a reference to the specified database object

 MongoDatabase database = server.GetDatabase("test"); // "test" is the database name

Get the specified collection, if there is no one in the database, a new one will be created

 MongoCollection col = db.GetCollection("Users");//Users collection name

Insert data into database

 public void Insert()
 {
     //Create database link
 MongoServer server = MongoDB.Driver.MongoServer.Create(strconn);
     //Get database test
 MongoDatabase db = server.GetDatabase(dbName);
     Users users = new Users();
     users.Name = "test";
     users.Sex = "man";
     //Get the Users collection, if there is no one in the database, create a new one first
 MongoCollection col = db.GetCollection("Users");
     //Perform insertion operation
 col.Insert(users);
 }

Update data

 public void Update()
 {
     //Create database link
 MongoServer server = MongoDB.Driver.MongoServer.Create(strconn);
     //Get database test
 MongoDatabase db = server.GetDatabase(dbName);
     //Get the Users collection
 MongoCollection col = db.GetCollection("Users");
     //Define the query conditions to obtain the "Name" value as "test"
 var query = new QueryDocument { { "Name", "test" } };
     //Define update document
 var update = new UpdateDocument { { "$set", new QueryDocument { { "Sex", "wowen" } } } };
     //Perform update operation
 col.Update(query, update);
 }

Delete data

 public void Delete()
 {
     //Create database link
 MongoServer server = MongoDB.Driver.MongoServer.Create(strconn);
     //Get database test
 MongoDatabase db = server.GetDatabase(dbName);
     //Get the Users collection
 MongoCollection col = db.GetCollection("Users");
     //Define the query conditions to obtain the "Name" value as "test"
 var query = new QueryDocument { { "Name", "test" } };
     //Perform deletion operation
 col.Remove(query);
 }

Query data

 public void Query()
 {
     //Create database link
 MongoServer server = MongoDB.Driver.MongoServer.Create(strconn);
     //Get database test
 MongoDatabase db = server.GetDatabase(dbName);
     //Get the Users collection
 MongoCollection col = db.GetCollection("Users");
     //Define the query conditions to obtain the "Name" value as "test"
 var query = new QueryDocument { { "Name", "test" } };
     //Query the data in all collections
 var result1 = col.FindAllAs();
     //Query the first piece of data with specified query conditions. The query conditions can be defaulted.
 var result2 = col.FindOneAs();
     //Query all data for specified query conditions
 var result3 = col.FindAs(query);
 } 

Summary

There are two ways to use collections: using the BsonDocument object model and using your own entity classes. This article mainly introduces using entities. If the data format is very arbitrary and it is difficult or impossible to define it as an entity class, use the BsonDocument object model. Since it is much easier to use your own entity class, and to determine the use of entities, your entity class must have the following requirements: have a no-argument constructor, for the data to be stored in��The data in the library needs to define public read/write fields or attributes. If the entity class is to be used as a root-level document, it must contain an Id field or attribute (usually named “Id”, although you can override it if necessary). Usually the type of Id is ObjectId.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/mongodb-add-delete-modify-2/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索