Introduction to NoSql Database MongoDB

Mongo is a high-performance, open source, schema-free document database that can be used to replace traditional relational databases or key-value storage in many scenarios. Mongo is developed in C++ and has the following features:

l Collection-oriented storage: suitable for storing data in the form of objects and JSON  .

l Dynamic query: Mongo supports rich query expressions. The query command uses JSON format markup to easily query objects and arrays embedded in the document.

l Complete index support: including document embedded objects and arrays. Mongo's query optimizer analyzes the query expression and generates an efficient query plan.

l Query monitoring: Mongo includes a monitoring tool for analyzing the performance of database operations.

l Replication and automatic failover: Mongo database supports data replication between servers, master-slave mode and mutual replication between servers. The primary goal of replication is to provide redundancy and automatic failover.

l Efficient traditional storage: supports binary data and large objects (such as photos or pictures).

l Automatic sharding to support cloud-level scalability (in early alpha stage): The automatic sharding feature supports horizontal database clusters with the ability to dynamically add additional machines.

Schema-free means that for a file stored in a mongodb database, we do not need to know any structure definition of it. If desired, you can store files of different structures in the same database.

Documents stored in a collection are stored as key-value pairs. The key is used to uniquely identify a document and is of type string, while the value can be of various complex file types. We call this storage form BSON (Binary Serialized dOcument Format).

The MongoDB server can run on Linux, Windows or OS X platforms, supports 32-bit and 64-bit applications, and the default port is 27017. It is recommended to run on a 64-bit platform, as the maximum file size supported by MongoDB when running in 32-bit mode is 2GB.

MongoDB stores data in files (the default path is: /data/db), and uses memory-mapped files for management to improve efficiency.

The main goal of MongoDB is to bridge the gap between key/value storage (which provides high performance and high scalability) and traditional RDBMS systems (rich functionality), combining the best of both worlds.

According to the description  on the official website , Mongo is suitable for the following scenarios:

l Website data: Mongo is very suitable for real-time insertion, update and query, and has the replication and high scalability required for real-time website data storage.

l Caching: Due to its high performance, Mongo is also suitable as a caching layer for information infrastructure. After the system is restarted, the persistent cache layer built by Mongo can avoid overloading the underlying data sources.

l Large size, low-value data: It may be expensive to store some data in a traditional relational database. Before that, programmers often chose traditional files for storage.

l High scalability scenarios: Mongo is very suitable for databases consisting of dozens or hundreds of servers. Mongo's roadmap already includes built-in support for the MapReduce engine.

l Storage of objects and JSON data: Mongo's BSON  data format is very suitable for storage and query in a documented format.

Naturally, the use of MongoDB will also have some limitations, for example it is not suitable for:

l Highly transactional systems: such as banking or accounting systems. Traditional relational databases are still more suitable for applications that require a large number of atomic complex transactions.

l Traditional business intelligence applications: BI databases for specific problems will generate highly optimized query methods. For such applications, a data warehouse may be a more appropriate choice.

l Questions that require SQL

MongoDB supports operating systems such as OS X, Linux, and Windows, and provides drivers for Python, PHP, Ruby, Java, and C++ languages. The community also provides drivers for platforms such as Erlang and .NET.

 

 

Like mysqld, a mongod service can have multiple databases, each database can have multiple tables, the table here is called collection, and each collection can store multiple documents (documents), and each document is BSON (binary) json) is stored on the hard disk. The difference from relational databases is that they are stored in single document units. You can add or delete fields to one or a batch of documents without affecting other documents. This is the so-called schema. -free, which is also the main advantage of document databases. Unlike the general key-value database, its value stores structural information, so you can read, write, and count certain fields like a relational database. It can be said that it has both the convenience and efficiency of the key-value database and the powerful functions of the relational database.

start the database

Download MongoDB  , unzip it and start:

$ bin/mongod

By default, MongoDB stores data in /data/db/ (or c:/data/db). Of course, you can change it to a different directory, just specify the --dbpath parameter:

$ bin/mongod --dbpath /path/to/my/data/dir

get database connection

Now we can use the built-in shell tool to operate the database. (We can also use the drivers of various programming languages ​​to use MongoDB, and the built-in shell tool can facilitate us to manage the database)

Start the MongoDB JavaScript tool:

$ bin / mongo

The default shell connects to the test library on the local localhost, and you will see:

MongoDB shell version: 0.9.8
url: test
connecting to: test
type "help" for help
>

“connecting to:” This will display the name of the database you are using. If you want to change the database you can:

> use mydb

You can type help to see all the commands.

insert data into collection

Let's create a collection of test and write some data. Create two objects, j and t, and save them to the collection.
In the example, '>' means the shell input prompt

> j = { name : "mongo" };
{"name" : "mongo"}
> t = { x : 3 };
{ "x" : 3  }
> db.things.save(j);
> db.things.save(t);
> db.things.find();
{"name" : "mongo" , "_id" : ObjectId("497cf60751712cf7758fbdbb")}
{"x" : 3 , "_id" : ObjectId("497cf61651712cf7758fbdbc")}
>

There are a few things to note:

  • There is no need to create a collection in advance. It will be created automatically when inserting data for the first time.
  • In the example, data of any structure can actually be stored, of course, in the actual application we store a collection of the same elements. This feature can actually be very flexible in the application, you do not need to modify your data structure like an alter table
  • Every time you insert data, the object will have an ID, called _id.

When you run different examples, your object ID values ​​are different.

Here's some more data:

> for( var i = 1; i < 10; i++ ) db.things.save( { x:4, j:i } ); > db.things.find();
{"name" : "mongo" , "_id" : ObjectId("497cf60751712cf7758fbdbb")}
{"x" : 3 , "_id" : ObjectId("497cf61651712cf7758fbdbc")}
{"x" : 4 , "j" : 1 , "_id" : ObjectId("497cf87151712cf7758fbdbd")}
{"x" : 4 , "j" : 2 , "_id" : ObjectId("497cf87151712cf7758fbdbe")}
{"x" : 4 , "j" : 3 , "_id" : ObjectId("497cf87151712cf7758fbdbf")}
{"x" : 4 , "j" : 4 , "_id" : ObjectId("497cf87151712cf7758fbdc0")}
{"x" : 4 , "j" : 5 , "_id" : ObjectId("497cf87151712cf7758fbdc1")}
{"x" : 4 , "j" : 6 , "_id" : ObjectId("497cf87151712cf7758fbdc2")}
{"x" : 4 , "j" : 7 , "_id" : ObjectId("497cf87151712cf7758fbdc3")}
{"x" : 4 , "j" : 8 , "_id" : ObjectId("497cf87151712cf7758fbdc4")}

Please note that the number of loops here is 10, but only 8 are displayed, and 2 pieces of data are not displayed.

If you want to continue to query the following data, you only need to use the it command, and the following data will continue:

{"x" : 4 , "j" : 7 , "_id" : ObjectId("497cf87151712cf7758fbdc3")}
{"x" : 4 , "j" : 8 , "_id" : ObjectId("497cf87151712cf7758fbdc4")}

continue

> it
{"x" : 4 , "j" : 9 , "_id" : ObjectId("497cf87151712cf7758fbdc5")}
{"x" : 4 , "j" : 10 , "_id" : ObjectId("497cf87151712cf7758fbdc6")}

Technically find() returns a cursor object. But in the above example, it doesn't get a cursor variable. So the shell automatically traverses the cursor, returns an initialized set, and allows us to continue iterating over the output with it.

Of course, we can also use the cursor to output directly, but this is the content of the next part.

Query data

Before we dig into the query, let's see how to return a cursor object from a query. You can simply query with find(), which returns a collection of any structure. If you implement a specific query, it will be explained later.

Implement the same query as above, then output via while:

> var cursor = db.things.find();
> while (cursor.hasNext()) { print(tojson(cursor.next())); }
{"name" : "mongo" , "_id" : ObjectId("497cf60751712cf7758fbdbb")}
{"x" : 3 , "_id" : ObjectId("497cf61651712cf7758fbdbc")}
{"x" : 4 , "j" : 1 , "_id" : ObjectId("497cf87151712cf7758fbdbd")}
{"x" : 4 , "j" : 2 , "_id" : ObjectId("497cf87151712cf7758fbdbe")}
{"x" : 4 , "j" : 3 , "_id" : ObjectId("497cf87151712cf7758fbdbf")}
{"x" : 4 , "j" : 4 , "_id" : ObjectId("497cf87151712cf7758fbdc0")}
{"x" : 4 , "j" : 5 , "_id" : ObjectId("497cf87151712cf7758fbdc1")}
{"x" : 4 , "j" : 6 , "_id" : ObjectId("497cf87151712cf7758fbdc2")}
{"x" : 4 , "j" : 7 , "_id" : ObjectId("497cf87151712cf7758fbdc3")}
{"x" : 4 , "j" : 8 , "_id" : ObjectId("497cf87151712cf7758fbdc4")}
{"x" : 4 , "j" : 9 , "_id" : ObjectId("497cf87151712cf7758fbdc5")}
>

The above example shows a cursor-style iterative output. The hasNext() function tells us if there is still data, and if so, we can call the next() function. Here we also use the built-in tojson() method to return a standard JSON format data.

When we are using the JavaScript shell, we can use the features of JS, and forEach can output the cursor. The following example is to use forEach() to loop the output:

> db.things.find().forEach( function(x) { print(tojson(x));});
{"name" : "mongo" , "_id" : ObjectId("497cf60751712cf7758fbdbb")}
{"x" : 3 , "_id" : ObjectId("497cf61651712cf7758fbdbc")}
{"x" : 4 , "j" : 1 , "_id" : ObjectId("497cf87151712cf7758fbdbd")}
{"x" : 4 , "j" : 2 , "_id" : ObjectId("497cf87151712cf7758fbdbe")}
{"x" : 4 , "j" : 3 , "_id" : ObjectId("497cf87151712cf7758fbdbf")}
{"x" : 4 , "j" : 4 , "_id" : ObjectId("497cf87151712cf7758fbdc0")}
{"x" : 4 , "j" : 5 , "_id" : ObjectId("497cf87151712cf7758fbdc1")}
{"x" : 4 , "j" : 6 , "_id" : ObjectId("497cf87151712cf7758fbdc2")}
{"x" : 4 , "j" : 7 , "_id" : ObjectId("497cf87151712cf7758fbdc3")}
{"x" : 4 , "j" : 8 , "_id" : ObjectId("497cf87151712cf7758fbdc4")}
{"x" : 4 , "j" : 9 , "_id" : ObjectId("497cf87151712cf7758fbdc5")}
>

forEach() must define a function to be called for each cursor element.

In the mongo shell, we can also use cursors as arrays:

> var cursor = db.things.find();
> print (tojson(cursor[4]));
{"x" : 4 , "j" : 3 , "_id" : ObjectId("497cf87151712cf7758fbdbf")}

When using cursors, please pay attention to the problem of occupying memory, especially large cursor objects, which may overflow the memory. Therefore, iterative output should be used.

The following example converts the cursor to a real array type:

> var arr = db.things.find().toArray();
> arr[5];
{"x" : 4 , "j" : 4 , "_id" : ObjectId("497cf87151712cf7758fbdc0")}

Note that these features are only available in the mongo shell, not all other application drivers.

MongoDB cursor objects are not without snapshots - if another user calls next() on the collection for the first or last time, you may not get the data in the cursor. So explicitly lock the cursor you want to query.

query with specified conditions

Now that we know how to implement a query from the cursor and return data objects, let's take a look at how to query based on the specified conditions.

The following example shows how to execute a SQL-like query and demonstrates how to implement it in MongoDB. This is a query in the MongoDB shell, but you can also implement it in other application drivers or languages:

SELECT * FROM things WHERE name="mongo"
> db.things.find({name:"mongo"}).forEach(function(x) { print(tojson(x));});
{"name" : "mongo" , "_id" : ObjectId("497cf60751712cf7758fbdbb")}
>
SELECT * FROM things WHERE x=4
> db.things.find({x:4}).forEach(function(x) { print(tojson(x));});
{"x" : 4 , "j" : 1 , "_id" : ObjectId("497cf87151712cf7758fbdbd")}
{"x" : 4 , "j" : 2 , "_id" : ObjectId("497cf87151712cf7758fbdbe")}
{"x" : 4 , "j" : 3 , "_id" : ObjectId("497cf87151712cf7758fbdbf")}
{"x" : 4 , "j" : 4 , "_id" : ObjectId("497cf87151712cf7758fbdc0")}
{"x" : 4 , "j" : 5 , "_id" : ObjectId("497cf87151712cf7758fbdc1")}
{"x" : 4 , "j" : 6 , "_id" : ObjectId("497cf87151712cf7758fbdc2")}
{"x" : 4 , "j" : 7 , "_id" : ObjectId("497cf87151712cf7758fbdc3")}
{"x" : 4 , "j" : 8 , "_id" : ObjectId("497cf87151712cf7758fbdc4")}
{"x" : 4 , "j" : 9 , "_id" : ObjectId("497cf87151712cf7758fbdc5")}
>

The query condition is { a:A, b:B, … } similar to “where a==A and b==B and …”, for more query methods, please refer to the Mongo development tutorial section.

The above shows all the elements, of course, we can also return specific elements, similar to returning the value of a field in the table, only need to specify the name of the element in find({x:4}), such as j:

SELECT j FROM things WHERE x=4
> db.things.find({x:4}, {j:true}).forEach(function(x) { print(tojson(x));});
{"j" : 1 , "_id" : ObjectId("497cf87151712cf7758fbdbd")}
{"j" : 2 , "_id" : ObjectId("497cf87151712cf7758fbdbe")}
{"j" : 3 , "_id" : ObjectId("497cf87151712cf7758fbdbf")}
{"j" : 4 , "_id" : ObjectId("497cf87151712cf7758fbdc0")}
{"j" : 5 , "_id" : ObjectId("497cf87151712cf7758fbdc1")}
{"j" : 6 , "_id" : ObjectId("497cf87151712cf7758fbdc2")}
{"j" : 7 , "_id" : ObjectId("497cf87151712cf7758fbdc3")}
{"j" : 8 , "_id" : ObjectId("497cf87151712cf7758fbdc4")}
{"j" : 9 , "_id" : ObjectId("497cf87151712cf7758fbdc5")}
>

Note that the "_id" element will always be returned.

findOne() – syntactic sugar

For convenience, the mongo shell (other drivers) avoids the possible overhead of cursors and provides a findOne() function. This function has the same parameters as find(), but it returns the first data in the cursor, or null for an empty database.

As an example, name=='mongo' can be implemented in a number of ways, either using next() to loop over the cursor (need to check for null), or returning the first element as an array.

But using the findOne() method is simpler and more efficient:

> var mongo = db.things.findOne({name:"mongo"});
> print(tojson(mongo));
{"name" : "mongo" , "_id" : ObjectId("497cf60751712cf7758fbdbb")}
>

The findOne method is more like find({name:”mongo”}).limit(1).

limit() query

You may need to limit the length of the result set by calling the limit method.

This is why high performance is strongly recommended, reducing network traffic by limiting the number of entries, for example:

> db.things.find().limit(3);
in cursor for : DBQuery: example.things ->
{"name" : "mongo" , "_id" : ObjectId("497cf60751712cf7758fbdbb")}
{"x" : 3 , "_id" : ObjectId("497cf61651712cf7758fbdbc")}
{"x" : 4 , "j" : 1 , "_id" : ObjectId("497cf87151712cf7758fbdbd")}
>

more help

In addition to general help, you can also query the help database and db.whatever for specific instructions.

If you want to do something with a function, you can output the source code of the implementation without entering the closing brackets {{()}}, for example:

> db.foo.insert
function (obj, _allow_dot) {
    if (!obj) {
        throw "no object passed to insert!";
    }
    if (!_allow_dot) {
        this._validateForStorage(obj);
    }
    return this._mongo.insert(this._fullName, obj);
}

mongo is a complete JavaScript shell program, so you can use JS methods, classes, and syntax privately in the shell. In addition, MongoDB defines many of its own classes and global variables (such as db). You can view the complete API description  here http: //api.mongodb.org/js/

 

http://blog.csdn.net/zclmoon/article/details/6215170

http://blog.csdn.net/zclmoon/article/details/6215171

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326986985&siteId=291194637