In the last issue, I introduced MongoDB and introduced the MongoDB installation method. In this issue, we will continue to follow the instructions of the author to implement basic data operations through the samus driver.
Traditional relational databases are generally composed of three levels: database, table, and record. MongoDB is composed of databases and collections) and document objects. MongoDB does not have columns, rows, and links in tables in relational databases. This shows the free mode.
I. MongoDB driver
MongoDB supports drivers in multiple languages. Here we only introduce the C # driver. There are many types of C # drivers, each of which has the same form, but each has its own details.CodeUnavailable. The official driver and samus driver are commonly used. In addition to common operations, the samus driver also supports data manipulation in the LINQ mode. Everyone prefers this method.
Drive: https://github.com/mongodb/mongo-csharp-driver/downloads
Samus DRIVER: https://github.com/samus/mongodb-csharp
This article explains database access, international practices, and "Hello World!" With the samus driver !".
2. Use the samus driver to implement helloworld access
Before proceeding to the following operations, make sure that the MongoDB service is enabled. You do not know how to enable the service. Please refer to the previous article. Download the driver, create a console project, and add a reference to MongoDB. dll. If you download the driver source code, compile and reference the generated DLL.
The basic code is as follows:
-
- // Link string
- String connectionstring = "MongoDB: // localhost ";
-
- // Database Name
-
- String databasename = "mydatabase ";
-
- // Set Name
-
- String collectionname = "mycollection ";
-
- // Define the Mongo Service
-
- Mongo = new Mongo (connectionstring );
-
- // Obtain the database corresponding to databasename. If the database does not exist, it is automatically created.
-
- Relational Database relational database = Mongo. getdatabase (databasename) as relational database;
-
- // Obtain the collection corresponding to collectionname. If the collection does not exist, it is automatically created.
- Collect collection <document> collect collection = collect database. getcollection <document> (collectionname) As collect collection <document>;
-
- // Link to the database
-
- Mongo. Connect ();
-
- Try
-
- {
-
- // Define a Document Object and store two key-value pairs
-
- Document Doc = new document ();
-
- Doc ["ID"] = 1;
-
- Doc ["MSG"] = "Hello world! ";
-
- // Insert this document object into the set
- Collections collection. insert (DOC );
-
- // Search for the document object whose key-value pair is id = 1 in the collection
-
- Document docfind = collections collection. findone (new document {"ID", 1 }});
-
- // Output the value corresponding to "MSG" in the searched document object, and output
-
- Console. writeline (convert. tostring (docfind ["MSG"]);
-
- }
-
- Finally
-
- {
-
- // Close the link
-
- Mongo. Disconnect ();
-
- }
RunProgram, Helloword is printed successfully. At the same time, we opened the data folder and found two more files, "mydatabase. Ns" and "mydatabase.0 ".
Iii. Summary
Code download: http://files.cnblogs.com/lipan/MongoDB_001.rar
This article briefly explains basic access operations. The next article will use the MVC framework to implement basic addition, deletion, query, modification operations for a single set at the model layer through MongoDB.
Source: http://www.cnblogs.com/lipan/archive/2011/03/08/1977691.html