MongoDB driver types
1. MongoDB official driver: mongo-csharp-driver,: https://github.com/mongodb/mongo-csharp-driver/downloads
2. Third-party drive samus, which is a type of driver that uses a lot of resources and is updated frequently. samus drivers support both normal operations and Linq and Lambda expressions. : Https://github.com/samus/mongodb-csharp.
The two mongodb drivers have the same operation procedure for the mongodb database, but there are differences in implementation methods. For example, the samus driver not only supports common operations, but also supports the expressions of Linq and Lambda.
Use the MongoDB driver to operate the database
Decompress the package to obtain the following two files:
MongoDB. Bson. dll: serialization, Json-related
MongoDB. Driver. dll: mongodb Driver
Add reference to introduce the above two DLL files to the Project
Introduce a namespace to the code
using MongoDB.Bson;using MongoDB.Driver;
Obtain the Database Connection Service
String connectionString = "mongodb: // localhost"; // mongodb: // [username: password @] hostname [: port] [/[database] [? Options] Login server = Login server. Create (connectionString); // connect to a login server
Obtains the reference of a specified database object.
Relational database = server. GetDatabase ("test"); // "test" is the database name.
Obtain the specified set. If the database does not exist, a new
Collect collection col = db. GetCollection ("Users"); // Users collection name
Insert data to database
Public void Insert () {// create a database link MongoServer server = MongoDB. driver. login server. create (strconn); // obtain the database test‑database db = server. getDatabase (dbName); Users users = new Users (); users. name = "test"; users. sex = "man"; // obtain the Users set. If the database does not exist, create a collection col = db. getCollection ("Users"); // execute the insert operation col. insert <Users> (users );}
Update Data
Public void Update () {// create database link Login server = MongoDB. driver. login server. create (strconn); // obtain the database test‑database db = server. getDatabase (dbName); // obtain the Users collection collections col = db. getCollection ("Users"); // define the query condition var query = new QueryDocument {"Name", "test"} for obtaining the "Name" value as "test "}}; // define the update document var update = new UpdateDocument {"$ set", new QueryDocument {"Sex", "wowen" }}}; // execute the update operation col. update (query, update );}
Delete data
Public void Delete () {// create database link Login server = MongoDB. driver. login server. create (strconn); // obtain the database test‑database db = server. getDatabase (dbName); // obtain the Users collection collections col = db. getCollection ("Users"); // define the query condition var query = new QueryDocument {"Name", "test"} for obtaining the "Name" value as "test "}}; // execute the delete operation col. remove (query );}
Query data
Public void Query () {// create database link Login server = MongoDB. driver. login server. create (strconn); // obtain the database test‑database db = server. getDatabase (dbName); // obtain the Users collection collections col = db. getCollection ("Users"); // define the query condition var query = new QueryDocument {"Name", "test"} for obtaining the "Name" value as "test "}}; // query the data var result1 = col in all sets. findAllAs <Users> (); // queries the first data of a specified query condition. The query condition can be set by default. Var result2 = col. FindOneAs <Users> (); // query all data of the specified query condition var result3 = col. FindAs <Users> (query );}
Summary
There are two methods to use a set: Use the BsonDocument object model and use your own object class. This document describes how to use entities. If the data format is casual and difficult to define or impossible to define as an object class, use the BsonDocument object model. Since it is much easier to use your own object class and you are sure to use the entity, your object class must have the following requirements: have a no-argument constructor, for data to be stored in the database, public read/write segments or attributes must be defined. If an object class is used as a root-level document, it must contain an Id field or attribute (usually named "Id", and you can override it even if necessary ). Generally, the Id type is ObjectId.