MongoDB Study Notes (2) use the samus driver to perform basic data operations

Source: Internet
Author: User
Tags mongodb driver

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 database), collection), and document object document) three layers. MongoDB has no concept of columns, rows, and links for tables in relational databases, which reflects 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. Therefore, the Code cannot be used in general. 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:

 
 
  1. // Link string
  2. String connectionString = "mongodb: // localhost ";
  3. // Database Name
  4. String databaseName = "myDatabase ";
  5. // Set Name
  6. String collectionName = "myCollection ";
  7. // Define the Mongo Service
  8. Mongo mongo = new Mongo (connectionString );
  9. // Obtain the database corresponding to databaseName. If the database does not exist, it is automatically created.
  10. Relational Database relational database = mongo. GetDatabase (databaseName) as relational database;
  11. // Obtain the collection corresponding to collectionName. If the collection does not exist, it is automatically created.
  12. Collect collection <Document> collect collection = collect database. GetCollection <Document> (collectionName) as collect collection <Document>;
  13. // Link to the database
  14. Mongo. Connect ();
  15. Try
  16. {
  17. // Define a Document Object and store two key-value pairs
  18. Document doc = new Document ();
  19. Doc ["ID"] = 1;
  20. Doc ["Msg"] = "Hello World! ";
  21. // Insert this document object into the set
  22. Collections collection. Insert (doc );
  23. // Search for the document object whose key-value pair is ID = 1 in the collection
  24. Document docFind = collections collection. FindOne (new Document {"ID", 1 }});
  25. // Output the value corresponding to "Msg" in the searched document object, and output
  26. Console. WriteLine (Convert. ToString (docFind ["Msg"]);
  27. }
  28. Finally
  29. {
  30. // Close the link
  31. Mongo. Disconnect ();
  32. }

Run the program and print helloword. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.