MongoDB from configuration to Application

Source: Internet
Author: User
Tags connection pooling failover install mongodb mongodb mongodb client mongo shell
Long wanted to write an article MongoDB, suffering from wood has time, today I have learned the MongoDB knowledge integration, share out, part of MongoDB knowledge from the network. No link here, if there is infringement, please contact me in time ... One, MongoDB introduction

Mongo (http://www.mongodb.org/) is a high-performance, open source (Dai Chun June is studying the source of MongoDB, we can go to see http://www.cnblogs.com/daizhj/), Model Freedom ( Schema-free), a document-type database that can be used in many scenarios to replace traditional relational databases or key/value (Key-value) storage methods. MONGO uses C + + development,

Has the following characteristics: collection-oriented storage : data suitable for storing objects and JSON forms. Dynamic Query : MONGO supports rich query expressions. Query directives use JSON-form tags to easily query objects and arrays that are embedded in the document. Full Index Support : Includes document inline objects and arrays. The MONGO query optimizer analyzes the query expression and generates an efficient query plan. Query Monitoring : MONGO contains a monitoring tool for analyzing the performance of database operations. replication and automatic failover : The MONGO database supports data replication between servers, supporting master-from-mode and server replication. The primary goal of replication is to provide redundancy and automatic failover. Efficient Traditional Storage : Supports binary data and large objects such as photos or pictures. automatic fragmentation to support cloud-level scalability (in the early alpha phase): Automatic fragmentation supports a level of database clustering that can dynamically add additional machines. mode Freedom (schema-free) means that we do not need to know any of its structural definitions for files stored in the MongoDB database. If necessary, you can store files of different structures in the same database. Supports Python,php,ruby,java,c,c#,javascript,perl and C + + language drivers, and the community also provides drivers for Erlang and. NET platforms.

Use: Site data: MONGO is very suitable for real-time inserts, updates and inquiries, and has the Web site real-time data storage needs of replication and high scalability. Caching: Because of its high performance, MONGO is also suitable as a caching layer for the information infrastructure. After the system restarts, the persistent cache layer built by MONGO can avoid overloading the underlying data source. Large size, low value data: It may be expensive to store some data using a traditional relational database, before which programmers often choose traditional files for storage. Highly scalable scenario: MONGO is ideal for databases composed of dozens of or hundreds of servers. The MONGO roadmap already contains built-in support for the MapReduce engine. Storage for objects and JSON data: MONGO's Bson data format is ideal for storing and querying in a documented format.

The so-called "set-oriented" (collenction-orented) means that data is grouped in a dataset and is called a set (Collenction). Each collection has a unique identification name in the database and can contain an unlimited number of documents. The concept of a set is similar to a table in a relational database (RDBMS), but it does not need to define any schema (schema). Two, Ubuntu under the configuration MongoDB

Ubuntu Knowledge Literacy:

Chown-r: The same permission changes are made to all files and subdirectories in the current directory (that is, to be changed one at a time in a recursive manner)

Nohup command: If you are running a process and you feel that the process will not end when you exit the account, you can use the Nohup command. This command can continue to run the process after you exit the account/close the terminal. Nohup is the meaning of not hanging (n ohang up).

MongoDB is currently the latest version of 1.8.1
Start the terminal first:
Enter Wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.8.1.tgz
Download MONGODB-LINUX-I686-1.8.1.TGZ installation package
Decompression TAR-ZXVF mongodb-linux-i686-1.8.1.tgz

By default MongoDB will store data in the/data/db/folder

Enter the following command in the current directory to create the user:
Add a MongoDB user
AddUser Mongodbxiao
Set Password
passwd MongoDB

Continue Typing

sudo mkdir-p/data/db/
To convert the owner of "/data/db/" into a MongoDB user
$ sudo chown mongodbxiao/data/db/
$ chown-r Mongodb:mongodbxiao/data
Of course, you can specify MONGODB to store the data in a different directory through the--dbpath command.

You need to install the MONGO shell before you run the following statement or you will receive a prompt: The program "MONGO" has not been installed.
Apt-get Install Mongodb-clients

Running the database
$./mongodb-linux-i686-1.8.1/bin/mongod
$./mongodb-linux-i686-1.8.1/bin/mongo
> Db.test.save ({123123: ' hahaha '})
> Db.test.find ();

Create the database and add a record, and the final query results:

To prove that we are configured MongoDB in Ubuntu successfully.

PS: This error is often encountered during the use of Ubuntu:

Just enter the force unlock and the command can be resolved.

sudo rm/var/cache/apt/archives/lock

sudo rm/var/lib/dpkg/lock III, MONGODB client management tools

Try MongoDB

You can try MongoDB without installing it, like Tryredis, a web-based shell simulation tool that allows you to try out MongoDB features without having to install MongoDB.

MONGO3–MONGODB Cluster Management Tools

Mongovue

A client management tool under Windows, I recommend this, very good. A bit like the MySQL admin tool.

Four, Mongodb C # version Drive

First paragraph: Mongodb-csharp Project address: Http://github.com/samus/mongodb-csharp

Simple introduction: The driver is a relatively full drive, the update is very fast, someone has been applied to the project, and performance is very good. Currently their team is working on the extension of the connection management feature such as automatic reconnection, connection pooling, and so on.

Second paragraph: mongodb-net Project address: http://code.google.com/p/mongodb-net/

Brief introduction: This development is still in progress, some features have not yet realized.

Section III: Mongodb.emitter Project address: http://bitbucket.org/gwbasic/mongodb.emitter/

Simple introduction: Strong type support provided

Fourth paragraph: Csmongo Project address: http://somewebguy.wordpress.com/2010/02/21/building-a-mongo-driver-part-1/

Simple introduction: Some of the features are implemented, and there is no downloadable code. But you can look at his blog to understand this kind of driving idea.

Fifth paragraph: Simple-mongodb Project address: http://code.google.com/p/simple-mongodb/

Brief introduction: No source code, with JSON as the core.

The last one is the enhancements to the first part of the section, address http://deserialized.com/ Convert-csharp-classes-to-and-from-mongodb-documents-automatically-using-net-reflection

Sixth paragraph: NoRM Project address: Http://github.com/atheken/NoRM

Introduction: Enhanced the first, strong type support. Five, MONGODB and SQL statement control

Here use the SQL statement in MySQL as an example, C # driver is Samus, which is the first one described above.

Introduce Project MongoDB.dll

Create a MONGO connection
  var mongo = new MONGO ("Mongodb://localhost");
  Mongo. Connect ();
  Gets a database, if no automatically creates a
  var db = Mongo. Getdatabase ("MovieReviews");
  Create a list and create a document for this list
  var movies = db. GetCollection ("movies");

After the connection is no problem, let's now compare the MySQL with MongoDB's statements:

Mongodb Mysql
Query all Movies.find (New Document ()) SELECT * FROM Movies
Conditional query Movies. Find (new Document {{"title", "Hello Esr"}}); SELECT * from movies WHERE title= ' Foobar '
Number of queries Movies. Find (new Document {{"title", "Test 2"}}). Documents.count (); SELECT COUNT (*) from movies WHERE ' title ' = ' Foobar '
Quantity Range Query 1, movies. Find (New Document (). ADD ("$where", New Code ("This.num > 50"));

2, movies. Find (New Document (). ADD ("num", new Document (). ADD ("$gt", 50));
($GT: > $gte: >= $lt: < $lte: <=; $ne:!=)

3,movies. Find ("This.num > 50");

4,movies. Find (New Document (). ADD ("$where", New Code ("function (x) {return this.num > 50};"));
SELECT * from movies where num > 50
Page-Search Movies. Find (New Document ()). Skip (10). Limit (20); SELECT * from movies Limit 10,20
Query Sort statement Movies. Find (New Document ()). Sort (New Document () {{"num",-1}}); SELECT * FROM Movies ORDER by Num DESC
Query-specific fields Movies. Find (New Document (). ADD ("num", new Document (). ADD ("$gt")), 0, New Document () {{"title", 1}}); Select title from movies where num > 50
Insert statement Movies. Insert (New Document () {{"title", "Test"}, {"Resuledata", DateTime.Now}}); INSERT inot Movies (' title ', ' Reauledate ') VALUES (' Foobar ', 25)
Delete statement Movies. Remove (New Document () {{"title", "Hello Esr"}}); DELETE * FROM Movies
UPDATE statement

Movies. Update (New Document () {{"title", Test 2}}}
, new Document () {{"title", "Test 11111"}});

UPDATE movies SET ' title ' = ' Test 1111 ' WHERE ' title ' = ' Test 1111 '
LINQ Query

(from item in DB.) GetCollection ("movies"). Linq ()
where ((string) item["title"]). StartsWith ("ESR")
Select item);

SELECT * from movies where title like '%ESR '

Ext: http://www.cnblogs.com/xiaogangqq123/archive/2011/04/26/2029426.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.