I. Mongodb introduction Mongo (www.mongodb.org) is a high-performance, open-source (DAI zhenjun daniu is studying Mongodb source code. You can go to www. cnblogs. comdaizhj), schema-free document-type database, which can be used in many scenarios to replace the traditional relational database or key-value (key-value)
A, Mongodb introduction Mongo (http://www.mongodb.org/) is a high-performance, open-source (DAI zhenjun Niu is studying Mongodb source code, you can go to look at the http://www.cnblogs.com/daizhj/), Mode free (schema-free) in many scenarios, it can be used to replace traditional relational databases or key-value (key-value)
I. Mongodb Introduction
Mongo (http://www.mongodb.org/) is a high-performance, open-source (dainiu is studying Mongodb source code, you can go to look at the http://www.cnblogs.com/daizhj/), free mode (schema-free) of the document type database, it can be used in many scenarios to replace traditional relational databases or key-value storage methods. Mongo is developed using C ++,
Has the following features:
- Set-oriented storage: Suitable for storing objects and JSON data.
- Dynamic query: Mongo supports multiple Query expressions. Query commands use JSON tags to easily query embedded objects and arrays in a document.
- Complete index support: Includes embedded object and array of documents. The Query Optimizer of Mongo analyzes the query expression and generates an efficient query plan.
- Query monitoring: Mongo contains a monitoring tool used to analyze the performance of database operations.
- Replication and automatic failover: The Mongo Database supports data replication between servers and master-slave mode and mutual replication between servers. The primary objective of replication is to provide redundancy and automatic failover.
- Efficient traditional storage: Supports binary data and large objects (such as photos or images ).
- Automatic sharding to support cloud-level scalability(In the early alpha stage): the auto-sharding function supports horizontal database clusters and allows you to dynamically add additional machines.
- Free mode(Schema-free) means that we do not need to know any structure definition of the files stored in the mongodb database. If necessary, you can store files of different structures in the same database.
- It supports drivers in Python, PHP, Ruby, Java, C, C #, Javascript, Perl, and C ++ languages. The community also provides Erlang and.. NET.
Usage:
- Website data: Mongo is ideal for real-time insertion, update, and query, as well as the replication and high scalability required for real-time website data storage.
- Cache: because of its high performance, Mongo is also suitable for serving as a cache layer for information infrastructure. After the system is restarted, the persistent cache layer established by Mongo can avoid data source overload at the lower layer.
- Large-sized and low-value data: traditional relational databases may be expensive to store some data. Previously, programmers often choose traditional files for storage.
- High scalability: Mongo is ideal for databases consisting of dozens or hundreds of servers. The Mongo roadmap contains built-in support for the MapReduce engine.
- For object and JSON Data Storage: Mongo's BSON data format is very suitable for storing and querying document-based data.
The so-called "Collenction-Orented" means that data is stored in a data set by groups and is called a collection ). Each set has a unique identification name in the database and can contain an infinite number of documents. The concept of a set is similar to that of a table in a relational database service (RDBMS). The difference is that it does not need to define any schema ).
2. Configure Mongodb in Ubuntu
Ubuntu knowledge Literacy:
Chown-R: Change the permissions of all files and sub-directories in the current directory in the same way (that is, change one by one in the way of delivery)
Nohup command: If you are running a process and you think the process will not end when you exit the account, you can use the nohup command. This command can continue running the corresponding process after you exit the account/Close the terminal. Nohup means not to suspend (n ohang up ).
The latest version of Mongodb is 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
Unzip tar-zxvf mongodb-linux-i686-1.8.1.tgz
By default, Mongodb stores data in the/data/db/folder.
Enter the following command in the current directory to create a user:
// Add a mongodb user
Adduser mongodbxiao
// Set the password
Passwd mongodb
Continue Input
Sudo mkdir-p/data/db/
// Change the "/data/db/" owner to a mongodb user
$ Sudo chown mongodbxiao/data/db/
$ Chown-R mongodb: mongodbxiao/data
Of course, you can use the-dbpath command to specify that MongoDB stores data in another directory.
Install the mongo shell before running the following statement. Otherwise, you will receive a prompt: The program "mongo" has not been installed.
Apt-get install mongodb-clients
Run Database
$? ./Mongodb-linux-i686-1.8.1/bin/mongod
$./Mongodb-linux-i686-1.8.1/bin/mongo
> Db. test. save ({123123: 'haha '})
> Db. test. find ();
Create a database and add a record. The query result is as follows:
This proves that we have successfully configured Mongodb in Ubuntu ..
Ps: this error is often encountered during Ubuntu:
You only need to enter the force UNLOCK command to solve the problem.
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 installation. Like tryredis, It is a web-based shell simulation tool that allows you to try MongoDB functions without installation.
Limit 3? -MongoDB cluster management tool
MongoVUE
I recommend a windows client management tool. It is a bit similar to mysql management tools ..
4. Mongodb C # driver
Article 1: mongodb-csharp Project address: http://github.com/samus/mongodb-csharp
Brief Introduction: the driver is a full-featured driver with fast update. It has been applied to the project and performs very well. At present, their team is working on expansion of connection management functions, such as automatic reconnection and connection pool.
Article 2: mongodb-net project address: http://code.google.com/p/mongodb-net/
Brief Introduction: The development is still in progress, and some features are not yet implemented.
Article 3 MongoDB. Emitter Project address: http://bitbucket.org/gwbasic/mongodb.emitter/
Brief Introduction: supports strong types
Section 4: CSMongo Project address: http://somewebguy.wordpress.com/2010/02/21/building-a-mongo-driver-part-1/
Brief Introduction: some functions are implemented and there is no downloadable code. But you can read his blog to understand this idea.
Article 5: simple-mongodb Project address: http://code.google.com/p/simple-mongodb/
Brief Introduction: source code is not provided, with JSon as the core.
The last one is the enhancement to the first one in some places, address http://deserialized.com/convert-csharp-classes-to-and-from-mongodb-documents-automatically-using-net-reflection
Article 6 NoRM project address: http://github.com/atheken/NoRM
Description: The first type is enhanced. Strong types are supported.
V. Comparison between Mongodb and SQL statements
Here we use the SQL statement in mysql as an example. The C # driver uses samus, which is the first one described above.
Introduce project MongoDB. dll
// Create a Mongo connection
var mongo = new Mongo("mongodb://localhost");
mongo.Connect();
// Obtain a database. If no database is created,
var db = mongo.GetDatabase("movieReviews");
// Create a list and create a document for this list
var movies = db.GetCollection("movies");
After the connection is complete, let's use mysql and mongodb statements for comparison:
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' |
Query quantity |
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 |
Paging Query |
Movies. Find (new Document (). Skip (10). Limit (20 ); |
SELECT * FROM movies? Limit 10, 20 |
Query sorting statements |
Movies. Find (new Document (). Sort (new Document () {"num",-1 }}); |
SELECT * FROM movies order by num DESC |
Query specified fields |
Movies. find (new Document (). add ("num", new Document (). add ("$ gt", 50), 10, 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', 'recorddate') 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 '% ss' |
Here are only a few typical examples. We can say that all SQL statements that mysql can complete can be implemented in mongodb.
Original article address: From Mongodb configuration to application, thanks to the original author for sharing.