MongoDB Learning Notes (ii) Implementation of basic data operation by Samus Drive _ server Other

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:

Copy Code code as follows:

Link string
String connectionString = "Mongodb://localhost";

Database name
String databaseName = "MyDatabase";

Collection name
String collectionname = "mycollection";

Defining MONGO Services
Mongo Mongo = new Mongo (connectionString);

Gets the database of the DatabaseName, which is automatically created if it does not exist
Mongodatabase mongodatabase = MONGO. Getdatabase (DatabaseName) as mongodatabase;

Gets the CollectionName corresponding collection, which is automatically created if no exists
mongocollection<document> mongocollection = mongodatabase.getcollection<document> (collectionName) as mongocollection<document>;

Link Database
Mongo. Connect ();
Try
{
Defines a document object that is stored in two key-value pairs
Document doc = new document ();
doc["ID"] = 1;
doc["MSG"] = "Hello world!";

Inserts this document object into the collection
Mongocollection.insert (DOC);

To find a Document object in the collection where the key value pair is id=1
Document docfind = Mongocollection.findone (new Document {{"ID", 1}});

Outputs the value of the key "MSG" in the document object that is found, and outputs
Console.WriteLine (Convert.ToString (docfind["MSG"));
}
Finally
{
Close link
Mongo. Disconnect ();
}

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://xiazai.jb51.net/201307/yuanma/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.

Author: Lee (Lipan)
Source: [Lipan] (http://www.cnblogs.com/lipan/)

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.