The first part MongoDB basic article

Source: Internet
Author: User
Tags mongodb server

What is NoSQL
Learn about MongoDB
Download and installation of MONGDB
The architecture of MongoDB
Common commands (basic additions and deletions)
Client GUI Tools Collection

One: What is NoSQL
1 Introduction to NoSQL
NoSQL is not the only SQL abbreviation, it refers to the non-relational database, is stored in key-value form, and the traditional off
System database is not the same, it does not necessarily follow the basic requirements of traditional databases, such as following the SQL standard, ACID properties, table
And so on, this kind of data mainly has the following characteristics, non-relational, distributed, open-source, horizontally extensible

2 NoSQL Development Status
The NoSQL sites currently being applied at home and abroad are:
Sina Weibo Redis
Google Bigtable
Vision China website MongoDB
Youku Operations Database Analysis MongoDB
Fetion Space Handlersocket
Wait a minute....

3 Why to use NoSQL
With the continuous development of the Internet, various types of applications are emerging, so in this era of cloud computing, the technology put forward
More demand, although the relational database has an unshakable position in the industry's data storage, but due to its innate
Several limitations make it difficult to meet the above requirements, expansion difficulties, read-write slow, high cost, limited support capacity, but
NoSQL is concerned with the high level of data read and write, and the storage of large amounts of data, compared to relational databases, they are
According to the library model, "subtraction" is done, and the addition is done in terms of extension and concurrency.


4 The pros and cons of NoSQL databases
In terms of advantages, mainly reflected in the following points:
Simple extension
Fast Read and Write
Low cost
Flexible data Model

In terms of inadequacy
does not provide support for SQL
Unsupported features are not rich enough
The existing product is not mature enough

Two realizations of MongoDB
1 MongoDB Introduction
MongoDB is a product between a relational database and a non-relational database, which is the most versatile and
Like relational databases, the syntax is somewhat similar to the JavaScript object-oriented query, which is a set-oriented, schema-free text
File Type Database

Set-oriented (collenction-orented)
This means that the database is grouped into a collection (collention), each set in the database has a unique
The identity name, and can contain an unlimited number of documents, the concept of the collection is similar to the table in relational data, the difference is that it does not need to define
Any mode

Mode Freedom (scheman-free)
Meaning that there is no concept of columns and rows in the collection, the following two records can exist in the same set
{"Name": "MONGO"}
{"Age": 25}

Document type (documents)
This means that the data we store is a set of key-values, the key is a string, and the value can be any type in the data type collection, including
Arrays and documents, each document equivalent to a record in a relational database

2 MongoDB Features
MongoDB is characterized by high performance, easy to deploy, easy to use, storage database is very convenient, the main features are
Collection-oriented storage for easy data storage of object classes
Mode freedom
Support Dynamic Query
Full index support, including internal objects
Support for replication and failure recovery
Use efficient binary data storage, including large objects (such as video, etc.)
Automatically process fragmentation to support scalability at the cloud level
File storage format Bson (an extension of JSON)

Applicable scenarios
In-place cache tiers
Efficient real-time performance
Storage for objects and JSON data
High-scalability Scenarios
Large-size, low-value data storage


Not suitable for the scene
Systems that require highly transactional
Traditional Business intelligence applications
Complex multi-table queries


Download and installation of three mongdb (Linux platform)
First step: Download the MongoDB installation package
MongoDB's official website: http://www.mongodb.org find the appropriate version to download
If you have a network, download it directly with the command.
wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.2.2.tgz

Step two: Unzip the compressed file
Tar zxvf mongodb-linux-i686-2.2.2.tgz

Step Three: Installation Preparation
Move MongoDB to the/usr/local/mongodb folder
MV Mongodb/usr/local/mongodb
Create a database folder (the default database file location is/data/db, which is created automatically at startup)

Mkdir/usr/local/mongodb/data
Hint: MongoDB does not have specific installation process, unzip the package, can be used directly, very efficient and convenient
Touch/usr/local/mongodb/dblogs
#日志文件


Fourth Step: Boot up
Adding a MongoDB boot project to rc.local ensures that MongoDB starts when the server is powered on
echo "/usr/local/mongodb/bin/mongod--dbpath=/usr/local/mongodb/data" >>
/etc/rc.local
Fifth step: Start MongoDB
Run the Mongod command
/usr/local/mongodb/bin/mongod--dbpath=/usr/local/mongodb/data--
Logpath=/usr/local/mongodb/dblogs--fork
--dbpath Execute Database storage path (default is/data/db)
--fork is run in daemon (process) mode
Note: If you specify the--fork parameter, you must specify the--logpath log file path

/usr/local/mongodb/bin/mongod--dbpath=/usr/local/mongodb/data--
Logpath=/usr/local/mongodb/dblogs--fork

Start command common three-digit option description
--dbpath the directory of the specified database
--port the port of the specified database, which is 27017 by default
--BIND_IP Binding IP
--DIRECTORYPERDB Create a separate subdirectory for each DB
--logpath
--logappend Specify how the log is generated (append/overwrite)
--PIDFILEPATH Specifies the process file path, and if it cannot be specified, the process file will not be generated
Key identification of--keyfile cluster mode
--journal Boot Log
--NSSIZE Specifies the size, in megabytes, of the. ns file, which is 16M by default and 2GB Max.
--maxconns Maximum number of concurrent connections
--notablescan does not allow table scanning
--noprealloc to close the pre-allocation function of database files

Sixth step: Enter the client operation
/usr/local/mongodb/bin/mongo

Seventh Step: Exit
Exit

Eighth step: Stop the MongoDB server
If the connection status is processed, you can stop it directly by sending the Db.shutdownserver () command in the Admin library

Unix System Directives
Killall Mongod
Note: Do not use the kill-9 pid to kill the MongoDB process, which will cause MongoDB database corruption, with KILL-2 kill
Dead process

The architecture of the four MongoDB
1 Comparison of logical structure relationships
MySQL data (database) Table rows three layers composed of

Non-relational database
MongoDB database (DB) collection (collection), Document Object (DOCUEMNT) three layers

The collection in MongoDB corresponds to a table in a relational database, but there are no columns, rows, and relationships in the collection, only documents in the collection
, a document is quite with a record, which embodies the characteristics of the freedom of the pattern

Show DBS; View the list of databases
db View Current Database
Show tables; View current Collection

2 Data storage structure
MySQL database structure
Each MySQL database is stored in a folder with the same name as the data, and if MySQL uses the MyISAM storage engine, the number
According to the library file type includes. frm, MYD, MYI

The sensitive data structure of MongoDB
The default data directory for MongoDB is/data/db it is responsible for storing all MONGODB data files in the MongoDB internal
, each database contains an. ns file and some data files, and these data files become more and more likely as data volumes pull pages
Most, so if there is a database called MyDB in the system, then the file that constitutes mydb this database main move by MYDB.NS,
mydb.0, mydb.1, etc.


3 MongoDB Data type
MongoDB Documents use Bson (Binary JSON) to organize data, Bson like JSON, JSON is just a simple representation
Data, which contains only the types of data (null, Boolean, number, string, array, and object) that are not fully complex
The business needs, therefore, the Bson also provides the date, 32 digits, 64 digits and other types, the following is the data type of MongoDB into the
Brief description of the line
1 null
A null type is used to represent a null value or a field that does not exist
For example: {"one": null}
2 Boolean type Tru False
3 32-bit integer MongoDB's console uses the JS engine for input, and JS supports only 64-bit floating-point numbers, so 32-bit full
Number will be automatically escaped
4 64-bit integer
5 64-bit floating-point number
6 string
7 symbols
8 Objectid type
9th issue
10 Regular Expressions
11 The Code document can contain JS code
12 arrays
13 Inline Document


Five common commands (basic additions and deletions)
Basic operations commands in the console

If you want to see where the database is currently connected, you can enter the DB directly
View user list db.system.users.find ();
View all user show users;
View all databases show DBS;
View all Collections Show collections
Deletes the current database Db.dropdatabase ();
Delete the collection db. Collection name. Drop ();
If you want to know what commands the current database supports, you can enter help directly
Want to know where the current database supports some methods: Db.help ();
Want to know where the current collection supports: Db.user.help (), user is the collection name

Create a database, collection
Because MongoDB is not a relational database file, in fact, it does not exist in the traditional relational database of so-called "database"
, when you add data for the first time, MongoDB will be saved and created as a collection collection without the need to mention
to establish

The first part of MongoDB basic article

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.