Reprinted from 1190000002547229 concept
MongoDBis a cross-platform, document-oriented database that provides high performance, high availability, and scalability.MongoDBwork on the concept of collection and documentation.
What is NoSQL?
NoSql, the full name isNot Only Sql, refers to the non-relational database. The next generation of databases mainly solves several key points: non-relational, distributed, open-source, horizontally extensible. The original aim was for large-scalewebapplications, the movement began in the early 2009, the general characteristics of applications such as: freedom of mode, support simple replication, simpleAPI, final consistency (nonACID), large capacity data and so on.NoSQLwe use the most when the number ofkey-valuestorage, of course, there are other document-type, column storage, graph database,xmldatabase and so on.
Why to use MongoDB
In the form of a JSON-style file, oriented to document storage: the data store can index and replicate any attribute and high availability automatically shards rich queries to quickly update MongoDB's professional technical support in place
Where should I use MongoDB?
Big Data Content Management and Delivery Mobile and Social Infrastructure User Data Management Data Platform
MongoDBis a product between a relational database and a non-relational database, the most versatile of the non-relational databases, most like relational databases. The data structures he supports are very loosely structured and are similarjsoninbsonformat, so you can store more complex data types .
The basic usage isJSONto store data, which is good forJavaScriptprograms. The characteristics are as follows:
1. There is no concept of table structure, each record can have a completely different structure
2. Business development is convenient and fast 3. SQL database needs to define the table structure before use
To install MongoDB under Mac
First, with browser or third-party tools to download
Current version of:http://downloads.mongodb.org/osx/mongodb-osx-x86_64-2.4.6.tgz
Or the second type, open the terminal, using the following command line to download:
curl http://downloads.mongodb.org/osx/mongodb-osx-x86_64-2.4.6.tgz > mongodb.tgz
After the installation succeeds, the“mongod”command starts themongodatabase. Browser input http://127.0.0.1:28017 IfmongoThe database information is displayed correctly, the startup is successful.
After the successful interface:
Installation steps
1. First enter in the terminalls, list the directory, find yourmongodbfile, totgzthe end of the
2. Open the terminal, entertar -zxvf mongodb-osx-x86_64-2.4.6, of course, the respective version number of the corresponding correct AH.
3. Move the extracted installation files to the location you like:
mv -n ~/Downloads/mongodb-osx-x86_64-2.4.6 ~/Applications/mongodb/
My unzip directory isDownloads, the default move toApplicationsthemongodbfolder is invisible
4. Create a directory at the root/below todata/dbplace themongodbdata and set permissions for the directory
sudo mkdir -p /data/dbsudo chown -R trigkit4 /data
5. Start themongodbservice
Open terminal, inputcd Applications/mongodb/bin, enter after, then input./mongod
6. Open another terminal, inputApplications/mongodb/bin, enter, enter./mongo
Document
A document is aMongoDBcore concept where multiple keys and their associated values are placed together in an orderly manner. Injs, the document is represented as an object:
{"greenting" : "Hello,world!"}
This object has only one key "greeting", and its corresponding value "Hello,world!"
The key of the document is a string, with the exception of a few exceptions, the key can use anyUTF-8character:
-The key cannot contain \ 0 (null character). This character is used to indicate the end of the key-. And $ have special meanings and can only be used under certain circumstances, and are generally reserved-keys beginning with an underscore "_" are reserved, although not strictly required
MongoDBNot only are types, but also case-sensitive, andMongoDBdocuments cannot have duplicate keys, for example, the following documents are illegal:
{"greeting":"Hello,world!","greeting":"Hello,mongoDB!"}
Collection
A collection is a set of documents. IfMongoDBthe document is similar to a row in a relational database, then the collection is like a table.
No mode
The collection is modeless. This means that the documents within the collection can be of various kinds, such as the following two documents can exist in the same set:
{"greeting": "Hello,world!"}{"foo": 5}
Named
We can mark the collection by name. The collection name can be a condition that meets the following conditionsUTF-8
-The collection name cannot be the empty string "". -Collection names cannot contain \ 0 characters (empty characters)-Collection names cannot begin with "system.", This is a prefix reserved for system collections-User-created collection names cannot contain the reserved character $
Sub-collection
A convention for organizing collections is to use a "." Separated subset of namespaces by namespace.
Basic operations in the shell
shell4 basic actions are used to view the data: Create, read, update, and delete (CRUD)
Database
MongoDBMultiple documents in a collection, as well as multiple collections to form a database. AnMongoDBinstance can host multiple databases, and the database name can be any string that meets the following criteriaUTF-8
-Cannot be empty string ("")-Cannot contain ‘‘ (space),., $, /, \, And \ 0 (empty characters)-All lowercase characters-Up to 64 bytes
Data type
MongoDBA list of support for many data types is given below:
String: This is the most commonly used data type to store data. Strings in MongoDB must be valid UTF-8. Integer: This type is used to store a value. The integer can be 32-bit or 64-bit, depending on your server. Boolean: This type is used to store a Boolean value (true / false). Double: This type is used to store floating point values. Min / Max keys: This type is used to compare the lowest and highest values of BSON elements. Arrays: Use this type of array or list or multiple values to store to a key. Timestamp: timestamp. This makes it easy to record when files have been modified or added. Object: This data type is used for embedded files. Null: This type is used to store a Null value. Symbol: This data type is used for strings that are the same, but it is usually reserved for languages with a specific symbol type. Date: This data type is a UNIX time format used to store the current date or time. You can specify your own date and time, date and year, month, and day to create objects. Object ID: This data type is used to store the ID of the document. Binary data: This data type is used to store binary data. Code: This data type is used for JavaScript code stored in the document. Regular expression: This data type is used to store regular expressions
MongoDB Basic Command Use command
MongoDB use DATABASE_NAMEUsed to create the database. The command creates a new database and returns the existing database if it does not exist.
Dropdatabase () method
MongoDB db.dropDatabase()The command is used to delete an existing database.
dropDatabase()The basic syntax of the command is as follows:
db.dropDatabase()
Drop () method
MongoDBdb.collection.drop()is used to delete a collection from the database.
Insert () method
To insert data into aMongoDBcollection, you need to useMongoDBtheinsert()orsave()method.
Find () method
The method that you want toMongoDBuse to gather data from the queryMongoDBfind().
Pretty () method
The results are displayed in a formatted way and can be used in apretty()method.
Limit () method
To limitMongoDBthe records in, you need to use alimit()method. Thelimit()method accepts a numeric parameter, which is the number of documents to display.
Grammar:
limit()The basic syntax of the method is as follows
>db.COLLECTION_NAME.find().limit(NUMBER)
MongoDB data dump
To createMongoDBa database in a backup, you should use themongodumpcommand.
mongodumpThe basic syntax of the command is as follows:
>mongodump
Recovering data
RestoresMongoDBthe commands used by the backup datamongorerstore. This command restores all the data from the backup directory.
Grammar:
mongorestoreBasic syntax for commands
>mongorestore
The rest of the method or command everyone can go online search, here will not repeat the AH.
Install MongoDB under Mac