MongoDB Installation and Basic use

Source: Internet
Author: User
Tags findone mongodb mongodb server reserved unique id unix domain socket mongo shell

The blog has moved Https://

MongoDB is an open-source, document-oriented NoSQL (not only SQL) database written using C + +, and one of the most popular NoSQL databases. NoSQL Introduction

NoSQL means "Not just SQL", is the current popular "non-relational database" collectively. Common NoSQL database such as: Redis, CouchDB, MongoDB, HBase, Cassandra and so on. background

Reasons for the NoSQL: to address the three high requirements in the Web2.0 era: the need for high levels of concurrent read and write to a database and the need for efficient storage and access to massive data for the high scalability and high availability of the database Rdb some of the features inside, It often becomes less important in web2.0, such as:
Database transaction Consistency Database real-time read-write complex SQL query, especially Multiple Table association query

From the 4th can see the use of MongoDB scene, but specific I focus on the following introduction. cap theorem


is also called the brewer theorem (Eric Brewer) It points out that for a distributed computing system, it is not possible to meet the following three points: strong consistency (consistency): The system is still in a consistent state of data after performing an operation, such as in a distributed system, When the update operation succeeds, all users should read the latest values, and such systems are considered strong consistency. Availability (availability): Each operation is always able to return results within a certain time partition fault tolerance (Partition tolerance): a single node failure should not cause the entire system to crash, that is, even though the network discards (or delays) any number of messages between nodes, But the system continues to operate. according to the CAP principle, the database is divided into three categories that satisfy the CA principle, satisfy the CP principle and satisfy the AP principle CA: Single point cluster, meet consistency, usability, usually less scalable, such as RDB CP: meet consistency and partitioning fault tolerance, usually not particularly high performance, such as distributed database AP: meet availability and partitioning fault tolerance, and generally may be less consistent requirements, such as most NoSQL BASE (basically available,soft-state,eventual consistency)

Ebay architect Dan Pritchett stems from a summary of the practice of large-scale distributed systems, published in ACM on the base theory, base theory is the extension of CAP theory, the core idea is that even if not strong consistency (strong consistency, Cap consistency is strong consistency, but applications can achieve final consistency in the appropriate way (eventual consitency). Basic available (basically Available): The system is able to run basically and always provide a soft state of service (Soft-state): The system does not require consistent strong final consistency (eventual Consistency): The system needs to achieve consistency after a certain moment the advantages of NoSQL are simple and convenient, especially horizontal scaling (vertical expansion refers to the use of a stronger machine; horizontal scaling means spreading data across multiple machines) Read-write fast and efficient, most will be mapped to the low cost of memory operation, with a common machine, distributed clustering can be data model flexibility, no fixed data model disadvantages do not provide support for the SQL transaction operations weak MongoDB use scenarios

I believe that a lot of people will feel, NoSQL good is good, but I should use in which circumstances, and how to combine with the actual project. Next I give a few examples, everyone experience. Game scene, using MongoDB storage game user information, user's equipment, integral and so on directly within the form of embedded documents stored, convenient to query, update the logistics scene, use MongoDB store order information, order status in the delivery process will be constantly updated to MongoDB in the form of embedded arrays to store, A query can read all the changes to the order. Social scene, the use of MongoDB storage to store user information, as well as the user published Friends circle information, through the geographical index to achieve the vicinity of people, places and other functional networking scenarios, using MongoDB storage of all access to intelligent device information, and equipment reported log information, And this information for multidimensional analysis of the video live, using MongoDB store user information, gift information, etc.

No business scenario has to be MongoDB to solve, but using MongoDB usually allows you to solve problems at a lower cost (including learning, development, operations, etc.), the following is the main features of MongoDB, you can compare your business needs to see, the more matching, with The MongoDB is more suitable.

MongoDB Characteristics Advantages
Transaction support MongoDB currently supports only single document transactions, scenarios that require complex transaction support are temporarily unsuitable
A flexible document model JSON format store closest to real object model, developer friendly, easy to quickly develop iterations
Highly Available replica sets Meet the data high reliability, high service availability requirements, simple operation, automatic failover
Scalable fragmented clusters Massive data storage, service capability level expansion
Performance Mmapv1, Wiredtiger, Mongorocks (ROCKSDB), in-memory, and other multiple engine support to meet the needs of various scenarios
Powerful index support Geo-indexing can be used to build a variety of O2O applications, text Index resolution search needs, TTL index to resolve historical data automatic expiration requirements
Gridfs Address the need for file storage
Aggregation & MapReduce Solve the data analysis scenario requirements, users can write their own query statements or scripts, the request will be distributed to the MongoDB completed

MongoDB installation method is relatively simple, I demonstrate the CentOS7 on the source code and Yum two ways to install. install using the Yum method

In the actual production environment, the software on the server is usually installed by the operator, we use this kind of fool installation is to facilitate the study now.

The entire MongoDB (Community Edition) contains the following software

Software name Description
Mongodb-org-server Contains the Mongod daemon and associated configuration and init scripts
Mongodb-org-mongos Contains the MONGOs daemon
Mongodb-org-shell Contains the MONGO shell, which is a command-line client that connects to MongoDB, allowing the user to enter the NOSQL syntax management database directly.
Mongodb-org-tools MongoDB with the following tools: Data import, export, backup, recovery, and so on

Creating Yum Source files

Copy the following to the file above
Name=mongodb Repository
Start the Yum command to start the installation
Yum Install-y mongodb-org
If you use SELinux, you must configure SELinux to allow MongoDB to be started on a Red Hat Linux based system (Red Hat Enterprise Linux or CentOS Linux).
#Set the value to disabled
Start MongoDB
#centos6 use this method
service mongod start

use this method
systemctl start Mongod

Check if the process started successfully
You can judge by viewing the MongoDB log file, open the/var/log/mongodb/mongod.log to see if there is any content in it. We can set the MongoDB service to boot Chkconfig mongod on.

Other control commands

#停mongodb service
Service Mongod stop
#restart mongodb
Service Mongod restart
command line Client connection to MongoDB service
#Enter the command directly after entering, you should be able to see the same hint as me

Root@bogon mongodb-linux-x86_64-rhel70-3.4.1]#./bin/mongo
MongoDB Shell version v3.4.1
Connecting to:mongodb://
MongoDB server version: 3.4.1
Welcome to the MongoDB shell.

#We can enter a command to test, now teach you one of the simplest commands to see what the current database has
> show dbs
Admin 0.000GB local 0.000GB
# You can see that there are two default databases, and if you want to exit the client you can enter
> exit

The first way to get here is to end the installation with the source code

Operators are more like the source code installation, they think controllability will be stronger, but now the company projects are using cloud services, so as a developer directly connected on the can be used. The source package I use here is actually binary package, which means that it has been compiled by the official, so when downloading the binary package you must determine the operating system you are using, otherwise the package is not available. Download the binary installation package and unzip

gunzip Mongodb-linux-x86_64-rhel70-3.4.1.tgz
tar-xvf Mongodb-linux-x86_64-rhel70-3.4.1.tar

#You should now see the same file directory as me.
│   ├──bsondump
│   ├──mongo
│   ├──mongod
│   ├──mongodump
│   ├── Mongoexport
│   ├──mongofiles
│   ├──mongoimport │
├──mongooplog │   ├── Mongoperf
│   ├──mongoreplay
│   ├──mongorestore │
├──mongos │   ├── Mongostat
│   └──mongotop
└── Third-party-notices
Create data storage directory, default is in/data/db, if this directory does not exist then start times wrong. You are free to create a directory that you can specify at startup.
Mkdir-p data/db1 #Start the service. /bin/mongod--dbpath=data/db1 #You should be able to see the same result as me 017-01-08t13:54:55.378+0800 I control [Initand Listen] 2017-01-08t13:54:55.378+0800 I control [Initandlisten] * * warning:access control isn't enabled for the database
. 2017-01-08t13:54:55.378+0800 I control [Initandlisten] * * * Read and Write access to data and configuration are UNR
Estricted. 2017-01-08t13:54:55.378+0800 I control [initandlisten] * * Warning:you are running this process as the root user, which I
s not recommended.
2017-01-08t13:54:55.378+0800 I control [Initandlisten] 2017-01-08t13:54:55.378+0800 i control [Initandlisten] 2017-01-08t13:54:55.378+0800 I control [Initandlisten] * * WARNING:/sys/kernel/mm/transparent_hugepage/enabled is '
Always '. 2017-01-08t13:54:55.378+0800 I control [Initandlisten] * * We suggest setting it to ' Never ' 2017-01-08t13:54:55.378 +0800 I control [Initandlisten] 2017-01-08t13:54:55.378+0800 i control [initandlisten] * * WARNING:/sys/kernel/mm/transParent_hugepage/defrag is ' always '. 2017-01-08t13:54:55.379+0800 I control [Initandlisten] * * We suggest setting it to ' Never ' 2017-01-08t13:54:55.379  +0800 I control [Initandlisten] 2017-01-08t13:54:55.446+0800 I ftdc [Initandlisten] initializing Full-time Data capture with directory ' Data/db1/ ' 2017-01-08t13:54:55.447+0800 I network [THREAD1] waiting for Conn Ections on Port 27017
Careful you should find a problem, the current service is started by the foreground, if you want to do the next operation that must open a terminal. This is certainly troublesome, then we can also use the background to start, at the start must specify a log file.
mkdir logs Touch

Here the source code installation and start MongoDB is over, the following on the start of the configurable parameters for an introduction. Basic Configuration

--quiet # Quiet Output--port arg # Specifies the service port number, the default port 27017 the--BIND_IP ARG # binding service IP, if bound, only native access, no default local all IP--logpath ARG # specified MO Ngodb log file, note that the specified file is not a directory--logappend # Use the Append method to write the log--pidfilepath arg # PID file full path, if not set, then no PID file--keyfile arg # cluster private key is complete Path, only valid--unixsocketprefix arg # UNIX domain socket substitution directory for replica Set schema, (Default/tmp)--fork # run MongoDB as daemon, create server process--auth # Enable authentication- -CPU # Periodically displays CPU utilization and iowait--dbpath arg # Specify database path--diaglog arg # diaglog option 0=off 1=w 2=r 3=both 7=w+some reads ORYPERDB # Set Each database will be saved in a separate directory--journal # enable logging option, MONGODB data operation will be written to Journal folder--journaloptions Arg # Enable logging diagnostics option--ipv6 # enable IPV6 option--jsonp # allow JSONP form to be accessed via HTTP (with security implications)--maxconns ARG # Maximum simultaneous connection number default \--noauth # do not enable authentication--nohttpinterface # Close HTTP connection Ports, default shutdown 27018 port Access--noprealloc # Disable data file pre-allocation (often affecting performance)--noscripting # Disable script engine--notablescan # do not allow table scans--nounixsocket # Disable UNIX socket Supervisor Listen to--nssize Arg (=16) # Set letter database. ns file Size (MB)--objcheck # on receipt of customer data, check validity,--profile arg # file Parameters 0=off 1=slow, 2=all--quota # Limits Number of files per database, set defaults to 8--quotaFiles arg # Number of files Allower per db, requires--quota--rest # Open Simple rest API--repair # fix all database run repair on all DB The directory of the files generated by the S--repairpath arg # Repair library, default to directory name DBPath--slowms arg (=100) # Value of slow for profile and console log--smallfile S # use smaller default file--syncdelay arg (=60) # data to disk in seconds (0=never, not recommended)--sysinfo # Print Some diagnostic system Information--upgrade # If you need to upgrade your database
Replicaton parameters
--fastsync # from a dbpath the Library Replication service is enabled, and the DBPath database is a snapshot of the main library, which can be used to quickly enable synchronization
--autoresync # If you synchronize data from the library to the main library and automatically resynchronize,
Oplogsize ARG # Set the size of the Oplog (MB)
Master, from parameter
--master # Main Library mode
--slave # from library mode
--source arg #
Specify a single database replication--slavedelay arg # from the Library port number--only ARG #
set the extension from the Library synchronization main Library Late time
Replica set
--replset ARG # set replica set name
--configsvr # declares that this is a cluster config service, the default port 27019, the default directory
--shardsvr # declares that this is a cluster fragment, the default port 27018
-- Nomoveparanoia # Off bigotry for movechunk data save

If you want to uninstall the installation MongoDB above, you can refer to the following steps. Of course this requirement will not be available in actual deployment, but it is also useful as a practice for us.

To completely remove MongoDB from the system, you must remove the MongoDB application itself, the configuration file, and any directories that contain data and logs. This process is not reversible, so make sure that you back up all the configuration and data before continuing.

Yum Erase $ (rpm-qa | grep mongodb-org) rm-r/var/log/mongodb rm-r/var/lib/mongo

If you are using the source code (binary) package installation, delete the extracted directory can (because we put the data and log in the above), if you specify in other locations, then find the appropriate location to delete. MongoDB Basic Use Basic Concepts Database
An instance of MongoDB can have one or more independent databases, each with its own collection (table). Collection
Collection can be thought of as a table- side file with dynamic mode
A document is a basic unit of data in a MongoDB, similar to a RDB row. A document is an ordered collection of key-value pairs. In JS, the document is represented as an object. _id
Each document has a special _id, which is unique in the collection of documents, and is maintained by the MongoDB itself, and you can specify it yourself, of course. JavaScript Shell
MongoDB has a powerful JavaScript Shell that can be used to manage or manipulate the conceptual comparisons of MongoDB MongoDB and Rdb
Have a concept set of the Database –>rdb table document –>RDB a record in a Document object key–> rdb a field in a table Document object Value–> RDB The value of a field in a table MongoDB a concept database name with no foreign key in the Call Definition Rule

1: cannot be empty string
2: Must not contain/, \,. , $, space, null characters, and so on, basically only use the letters in ASCII and the number
3: case-sensitive, recommended all lowercase
4: Up to 64 bytes
5: You must not use reserved database names, such as: Admin,local,config

Note: The database will eventually become a file, the database name is the name of the file collection name definition rule

1: cannot be empty string
2: Can not contain a character (null character), this character represents the end of the collection name, also cannot contain "$"
3: cannot be "system." Beginning, this is the prefix reserved for the system collection
rules for defining the keys of a document
1: Cannot contain a "character" (null character), this character represents the end of the key
2: "." and "$" are reserved and can only be used in a specific environment with
3: Distinguishing types, but also case-sensitive
4: keys cannot be duplicated

Note: The document's key-value pairs are sequential, and the same key-value pairs, if they are in different order, are also different documents MongoDB basic data Types

Data Type Description examples
Null Represents a null value or an object that is not defined {"X": null}
Boolean value True or false: TRUE or False {' X ': true}
32-bit integer Shell does not support this type, it is converted to 64-bit floating-point numbers by default, or you can use the Numberint class {"X": Numberint ("3")}
64-bit integer Shell does not support this type, it is converted to 64-bit floating-point numbers by default, or you can use the Numberlong class {"X": Numberlong ("3")}
64-bit floating-point numbers The number in the shell is this one type {"X": 3.14, "Y": 3}
String UTF-8 string {' foo ': ' Bar '}
Symbol Shell does not support, the shell automatically converts data from a symbol type in a database to a string
Object ID 12-byte Unique ID of the document {"id": ObjectId ()}
Date The number of milliseconds since the standard era {' Date ': new Date ()}
Regular expressions The document can contain regular expressions, followed by JavaScript syntax {' foo ':/foobar/i}
Code The document can contain JavaScript code {' X ': function () {}}
Not defined Undefined {"X": undefined}
Array The collection or list of values {"Arr": ["a", "B"]}
Inline document A document can be used as the value of a key in a document {"X": {"foo": "Bar"}}
additions and deletions (CUD) Operation

Next I manipulate the data in the command-line client, assuming you've entered the command line. If you are not in sync with me, please take another look at the tutorials above.

# run Shell, command: MONGO Ip:port, here I use the default port so there is no designation [Root@bogon mongodb-linux-x86_64-rhel70-3.4.1]#./bin/mongo # Display an existing database
, command: Show DBS; > Show DBS Admin 0.000GB Local 0.000GB # Display the database currently in use, command: DB > DB Test # Toggle the currently used database, command: Use database name > Using local SWI
tched to DB Local # Create a database: MongoDB does not have a database-specific statement, you can use a database using use, and if the database you are using does not exist, you will create one that will be saved as a file after the document is actually added to the library.
> Use mall switched to DB Mall # Delete database, command: Db.dropdatabase (), before using this command to remember to switch to the deleted data, you can use the command learned above to confirm. > DB Mall > Db.dropdatabase () {"Dropped": "Mall", "OK": 1} # Display an existing collection, command: Show collections, let's switch to a new database and insert a collection into it.
and save a piece of data, just to make a statement that makes it easier to demonstrate the command.
> Use mall switched to DB Mall > Db.user.insert ({name: ' John '}) Writeresult ({"ninserted": 1}) > Show collections User # Create a collection: You do not have to create a collection in MongoDB, because there is no fixed structure, directly using the DB. Collection name. command to operate on it.
If you want to display the creation set, use: Db.createcollecion (' Set name '); > db.createcollection (' System ') {"OK": 1} # Inserts and saves the document # Insert method, you can insert a single document, or you can insert multiple, with []. Note: # 1:mongodb will automatically add a "_id" field for each document that does not have a "_id" Field # 2: Each doc must be less than 16MB # There's already a demo above, there's no more going on here. Delete document, command: Remove, can be deleted by condition just delete the document, the collection is still in, if using the drop command, will be associated with the collection and the index deleted # View all documents in the collection, command: DB. Collection name. Find (); > Db.user.find () {"_id": ObjectId ("5871E5F99423674EDCEA4EEC"), "name": "John"} > Db.user.remove ({}) Writeresult ({
"Nremoved": 1}) > Db.user.find () > # View the status information for the collection: DB. Collection name. Stats (); > Db.user.stats () {"ns": "Mall.user", "size": 0, "Count": 0, "storagesize": 20480, "capped":

False, "Wiredtiger": {...}, ...}
    # View the first document in the collection, command: DB. Collection name. FindOne ({Conditional object}), making some data convenient to test > Db.user.insert ([{name: ' Dick '},{name: ' Harry '}]) Bulkwriteresult ({ "Writeerrors": [], "writeconcernerrors": [], "ninserted": 2, "nupserted": 0, "nmatched": 0, "NM Odified ": 0," nremoved ": 0," upserted ": []}) > Db.user.findOne () {" _id ": ObjectId (" 5871e8fe9423674edcea4 Eed ")," name ":" Dick "} # Document replacement, command: DB. Collection name.
Update (condition, new document); > db.user.update ({name: ' Harry '}, {name: ' Harry ', Sex: ' Man '}) Writeresult ({"nmatched": 1, "nupserted":0, "nmodified": 1}) > Db.user.find () {"_id": ObjectId ("5871e8fe9423674edcea4eed"), "name": "Dick"} {"_id": Object

Id ("5871e8fe9423674edcea4eee"), "name": "Harry", "Sex": "Men"} # The above instructions: first find the name is Harry this record, and then replace with the new document all the original value of the data, is a whole replacement operation. # Save Method # If the document exists on the update, does not exist on the new, mainly according to the "_id" to judge.
You can see that we inserted a document with the same name, and the result was a success. > ({name: ' Dick '}) Writeresult ({"ninserted": 1}) > Db.user.find () {"_id": ObjectId ("5871e8fe9423674edc Ea4eed ")," name ":" Dick "} {" _id ": ObjectId (" 5871e8fe9423674edcea4eee ")," name ":" Harry "," Sex ":" Man "} {" _id ": ObjectId ( "5871eb8e9423674edcea4eef"), "name": "Dick"} # Upsert # found a document that matches the criteria, or it will create a new document with this condition and update the document. Specifies that the third parameter of the Update method is true, representing the Upsert > db.user.update ({name: "John"}, {name: "John", Sex: ' Female '}) Writeresult ({"nmatched": 0, "  Nupserted ": 0," nmodified ": 0}) > Db.user.find () {" _id ": ObjectId (" 5871e8fe9423674edcea4eed ")," name ":" Dick "} {
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: 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.