+ MongoDB MongoDB theoretical knowledge to install and start using MongoDB common commands +

+ MongoDB MongoDB theoretical knowledge to install and start using MongoDB common commands +

This blog speak with everyone understand the theoretical knowledge of MongoDB, then install MongoDB window system, under Docker environment MongoDB installation.

What is MongoDB

MongoDB is written in C ++ language, it is an open source database distributed file system based storage.

MongoDB also can be said to be a cross-platform, document-oriented database, NoSQL database products are currently the most popular one. It ranged between relational databases and non-relational databases, non-relational database functions among the richest and most products like relational databases. It supports data structure is very loose, the BSON JSON format is similar, it is possible to store more complex data types.

In the case of high load, add more nodes, you can ensure server performance.

MongoDB is designed to provide scalable, high-performance data storage solution for WEB applications.

MongoDB the data is stored as a document data structure of the key (key => value) pairs. MongoDB document similar to JSON object. Field value can contain other documents, arrays and array of documents.

MongoDB's official website address is: http://www.mongodb.org/

MongoDB features

MongoDB biggest feature is the query language supported by his very powerful, its syntax is somewhat similar to the object-oriented query language, most of the functionality can be achieved almost single-table queries similar to a relational database, but also support for data indexing. It is a set-oriented, free model document database. Specific features are summarized as follows:

(1) for collection storage, easy to store types of data objects

(2) a free mode

(3) support for dynamic queries

(4) support fully indexed, contain internal objects

(5) supports replication and failover

(6) using an efficient binary data storage, including large objects (e.g., video, etc.)

(7) automatically handles fragmentation, in order to support the expansion of the cloud level

(8) support Python, PHP, Ruby, Java, C, C #, Javascript, Perl driver and the C ++ language,
the community also provides Erlang and .NET drivers for other platforms

(9) The file format is stored in the BSON (one kind of extension JSON)

Architecture MongoDB

MongoDB logical structure is a hierarchical structure. Mainly by:

Document (document), collection (collection), database (database) This three-part. Logical structure is user-oriented, user MongoDB develop applications that use is the logical structure.

(1) MongoDB document (document), equivalent to a row in a relational database.

(2) composed of a plurality of documents set (collection), equivalent relational database table.

A plurality (3) collection (Collection), grouped together logically, that the database (database).

(4) a plurality of support MongoDB instance database (database).

Document (document), a collection (collection), database (Database) hierarchy as shown below:


The following table is a comparison MongoDB concept MySQL database logical structure

MongoDB data type

Basic data types

null: null value or for indicating the absence of a field, { "x": null}

Boolean: There are two types of Boolean values ​​true and false, { "x": true}

Value: shell 64 is a default value float. { "X": 3.14} or { "x": 3}. For integer values ​​may be used NumberInt (4 byte unsigned integers) or NumberLong (8-byte signed integer), { "x": NumberInt ( "3")} { "x": NumberLong ( "3")}

String: UTF-8 can be expressed as a string data type string, { "x": "Oh"}

Date: The date is stored as the number of milliseconds elapsed from the dependent era, the time zone is not stored, { "x": new Date ()}

Regular expression: query, using regular expressions as defined conditions, JavaScript syntax same regular expression, { "x": / [abc] /}

Array: data list or data set may be represented as an array, { "x": [ "a", "b", "c"]}

Embedded documents: Document other documents can be nested, nested treated as a value document, { "x": { "y": 3}} Object Id: Object id is a 12-byte character string, the document is the unique identifier, { "x": objectId ()}

Binary data: the data is a binary string of arbitrary byte. It can not be used directly in the shell. If you want to save the non-utf- characters into the database, binary data is the only way.

Code: query and a document may include any JavaScript code, { "x": function () {/ ... /}}

What kind of features suitable for use MongoDB

And two Tucao comments feature has the following features:

(1) the amount of data

(2) write operation frequently

(3) lower value

For such data, we use MongoDB for storing data to achieve

Installation and start MongoDB

window installation system MongoDB

Double-click "mongodb-win32-x86_64-2008plus-ssl-3.2.10-signed.msi" follow presentation step install. After installation, the software will be installed in C: \ Program Files \ MongoDB directory.

We want to start the service program is C: \ \ MongoDB \ 3.2 under the Program Files Server \ \ bin directory mongod.exe, in order to facilitate every time we start, I will be C: \ Program Files \ MongoDB \ Server \ 3.2 \ bin settings the environment variable path.

start up

(1) First, open a command prompt, create a directory to store data for a

md d:\data

(2) to start the service

mongod ‐‐dbpath=d:\data(data文件的路径)

We can see in the start information, the default port is 27017 mongoDB

If we want to change the default startup port, the port can be specified by the -port

At the command prompt, enter the following command to complete the landing

mongo

Exit mongodb

exit
MongoDB installed at Docker environment

Create a container in mongo host

docker run --name mongodb -p 27017:27017 -d mongo


Remote login

Open cmd in the bin directory, enter the command

mongo 192.168.184.134

MongoDB commonly used commands

Select the format and syntax to create a database: use the database name

If the database does not exist is automatically created

The following statement creates student database

use student

Insert the document syntax: db collection name .insert (data).

The following statement into the document data

db.student.insert({"_id":1,"name":"宋政宏","sex":"男","age":25,"introduce":"帅哥"})

Queries collection syntax: db collection name .find ().

If we want to query all documents in a collection of student, we enter the following command:

db.spit.find()

Here you will find every document will have a field named _id. This is equivalent to the primary key of the table in our original relational database, when you do not specify the field when inserting documentation, MongoDB is automatically created, its type is the type ObjectID . If we specify the field at the time of recording can also be inserted into the document, it can be a type ObjectID type, it can be any type of support for MongoDB.

As shown above operations:
insert here described image
input statements the following tests:

db.student.insert({"_id":2,"name":"吴语","sex":"男","age":23,"introduce":"中庸"})
db.student.insert({"_id":3,"name":"华楠","sex":"女","age":21,"introduce":"女神"})
db.student.insert({"_id":4,"name":"山蒲","sex":"男","age":24,"introduce":"小胖"})
db.student.insert({"_id":5,"name":"穆斯","sex":"男","age":26,"introduce":"男神"})

If I want to query certain conditions, for example, I would like to check age of 25 for the record, how do? Very simple! Just add parameters to the parameter is json format () in the find, as follows:

db.student.find({"age":25})

If you only need to return the first qualifying data, we can use the command to achieve findOne

db.student.findOne({"age":25})

If you want to return the specified number of records, you can call limit after the find methods to return the results, such as:

db.student.find().limit(3)

Modify the grammatical structure of the document: db collection name .update (condition, the modified data).

If we want to change _id record 1, aged 26, enter the following statement:

db.student.update({_id:"1"},{"age":NumberInt(26)})

After the execution, we will find this document in addition to age field other fields are gone, in order to solve this problem, we need to use modifiers $ set to achieve the following command:

db.student.update({_id:"2"},{$set:{"age":NumberInt(26)}})

Delete documents grammatical structure:. Db collection name .remove (condition)

The following statement will delete all the data, caution

db.student.remove({})

If you remove the "age" = 26 record, enter the following statement

db.student.remove({"age":26})

Statistical recording conditions using the count () method. The number of records following statement sets of statistics student

db.student.count()

If the statistics according to criteria such as: age statistics for the number of records 26

db.student.count({"age":26})

MongoDB fuzzy query is achieved by means of regular expressions. The format is: / fuzzy query string /

For example, I have to find all documents with the name of "Song", the code is as follows

db.student.find({"name":/政/})

If you want to query the beginning of the name in the "Song", the code is as follows:

db.student.find({"name":/^宋/})

Greater than equal to not less than
<, <=,>,> = this operator is very common in the following format:

db.集合名称.find({ "field" : { $gt: value }}) // 大于: field > value

db.集合名称.find({ "field" : { $lt: value }}) // 小于: field < value

db.集合名称.find({ "field" : { $gte: value }}) // 大于等于: field >= value

db.集合名称.find({ "field" : { $lte: value }}) // 小于等于: field <= value

db.集合名称.find({ "field" : { $ne: value }}) // 不等于: field != value

Example: query records older than 25

db.student.find({"age":{$gt:25}})

It contains does not contain

It comprises the use of $ in operator.
Example: Query field contains the age of 25 and 26 documents

db.student.find({"age":{$in:[25,26]}})

It does not include the use of $ Nin operator.
Example: Query age field does not contain documents of 25 and 26

db.student.find({"age":{$nin:[25,26]}})

Conditions connections

If we need to query meet two or more conditions, the operator need to use the $ and the associated conditions. (The equivalent SQL and)
format: $ and: [{}, {}, {}]
Example: Query document age greater than 23 and less than 26

db.student.find({$and:[ {"age":{$gte:23}} ,{"age":{$lt:26} }]})

If a relationship between two or more conditions, we use the operator or be associated, as in the previous embodiment and using
the format: $ or: [{}, {}, {}]
Example: Query sex is "Male "documentation or age less than 25

db.student.find({$or:[ {"sex":"男"} ,{"age":{$lt:25} }]})

Column value growth
if we want to achieve on a column values increase or decrease in value on the basis of the original, you can use the $ inc operator to achieve

db.student.update({_id:"2"},{$inc:{"age":NumberInt(1)}}	)

In fact, the column values ​​increase with thumbs up with more features

Huawei cloud [cloud] in huge benefits: registered there

Guess you like

Origin blog.csdn.net/DLLLL_/article/details/91948079