MongoDB Learning Notes (ii) Implementation of basic data operation through Samus Drive

Source: Internet
Author: User
Tags mongodb
Traditional relational databases are generally composed of three levels of databases (database), tables (table), records (record), and MongoDB are made up of three levels (db), Collections (collection), document objects (documents). MongoDB for tables in relational databases, but there is no column, row, and relationship concept in the collection, which embodies the characteristics of pattern freedom. one, about the MongoDB drive

MongoDB supports multiple language drivers, here we only introduce C # drivers. There are many types of C # drivers, each with roughly the same form, but the details are different, so the code is not universal. The more commonly used is the official drive and the Samus drive. The Samus driver supports LINQ manipulation of data in addition to general forms of operation. Everyone prefers this way.

Official Driver Download Address: Click to download
Samus Driver Download Address: Click to download

This article will start from Samus drive to explain the database access, international practice, Access "Hello world!". second, through Samus Drive to realize HelloWorld access

Before doing the following, please make sure the MongoDB service is turned on, do not know how to open the service, please see article. Download the driver, create a new console item, and add a reference to the MongoDB.dll, if you download the driver source code, compile it again to refer to the generated DLL.

The basic code is as follows: View source print?

01 Link string
02 String connectionString = "Mongodb://localhost";
03
04 Database name
05 String databaseName = "MyDatabase";
06
07 Collection name
08 String collectionname = "mycollection";
09
10 Defining MONGO Services
11 Mongo Mongo = new Mongo (connectionString);
12
13 Gets the database of the DatabaseName, which is automatically created if it does not exist
14 Mongodatabase mongodatabase = MONGO. Getdatabase (DatabaseName) as mongodatabase;
15
16 Gets the CollectionName corresponding collection, which is automatically created if no exists
17 mongocollection<document> mongocollection = mongodatabase.getcollection<document> (collectionName) as mongocollection<document>;
18
19 Link Database
20 Mongo. Connect ();
21st Try
22 {
23 Defines a document object that is stored in two key-value pairs
24 Document doc = new document ();
25 doc["ID"] = 1;
26 doc["MSG"] = "Hello world!";
27
28 Inserts this document object into the collection
29 Mongocollection.insert (DOC);
30
31 To find a Document object in the collection where the key value pair is id=1
32 Document docfind = Mongocollection.findone (new Document {{"ID", 1}});
33
34 Outputs the value of the key "MSG" in the document object that is found, and outputs
35 Console.WriteLine (Convert.ToString (docfind["MSG"));
36 }
37 Finally
38 {
39 Close link
40 Mongo. Disconnect ();
41 }

Run the program to successfully print Helloword. At the same time, we opened the Data folder, found more than two files "Mydatabase.ns" and "mydatabase.0." Third, summary

Code Download: Http://files.cnblogs.com/lipan/MongoDB_001.rar

This concise explanation of the basic access operation, the next article will be combined with the MVC framework through the MONGODB to achieve the model layer of a single set of basic additions and deletions to check the operation.

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.