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

Source: Internet
Author: User
Tags mongodb 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 has no concept of columns, rows, and links for tables in relational databases, which reflects the free mode.

Series directory

MongoDB Study Notes (1) Introduction and installation of MongoDB
MongoDB Study Notes (2) use the samus driver to perform basic data operations
MongoDB learning notes (3) using jqgrid tables to operate MongoDB data in MVC Mode
MongoDB learning notes (4) describe the data relationship using the document structure of MongoDB
MongoDB Study Notes (5) MongoDB File Access Operations
MongoDB Study Notes (6) MongoDB index usage and Efficiency Analysis

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.CodeNot applicable. 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.

Official DRIVER: Click to download
Samus DRIVER: Click to download

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 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, the system automatically creates the required database = Mongo. getdatabase (databasename) as your database; // gets the collection corresponding to collectionname. If no collection exists, the custom collection is automatically created. 
  
    m Ongocollection = External Database. getcollection 
   
     (collectionname) as your collection 
    
     ; // link to the database Mongo. connect (); try {// define a Document Object and store the two key-value pairs document DOC = new document (); Doc ["ID"] = 1; doc ["MSG"] = "Hello world! "; // Insert this document object into the collection collections collection. insert (DOC); // search for the Document Object document docfind = Hangzhou collection whose key-value pair is id = 1 in the collection. findone (new document {"ID", 1}); // output the value corresponding to "MSG" in the searched document object, and output the console. writeline (convert. tostring (docfind ["MSG"]);} finally {// close the Mongo link. 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, query, modification, and modification operations for a single set at the model layer through MongoDB.

By Lipan)
Source: [Lipan] (http://www.cnblogs.com/lipan)
Copyright: The copyright of this article is shared by the author and the blog. The detailed link of this article must be noted during reprinting; otherwise, the author will be held legally liable.
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.