(A), MongoDB composition
database, a collection of document, domains, index SQL terms MongoDB Term Explanation / Description database database database table collection database tables / set row document data rows / document column field data field / fields index index index table joins table connection, MongoDB does not support the primary key primary key primary key, MongoDB automatically _id field as the primary key
1, the database. mongodb can create multiple databases. MongoDB single instance can accommodate a plurality of individual databases, each with its own set of permissions and, different databases are also placed in different files. <1>, common command show dbs "command to display a list of all the data. Execution" db "command to display the current database or a collection of objects. Use yz create the database. Db.yz.insert ({" name "," liqingbiao " }) insert data db.dropDatabase () to the database you just created; delete database
2, the system keeps a database
admin: From the perspective of rights, this is "root" database. If a user is added to the database, the user automatically inherits all the permissions database. Some specific server-side command can only be run from the database, such as a list of all database or server. local: This data will never be copied, can be used to store any set of limited config single local server: when Mongo provided for fragmentation, config databases use for storing fragmentation related information.
3, document. Documentation is a key-value pair (key-value). mongodb documents do not need to set the same field, the same field and does not require the same type of data, relational databases, and this is quite different. 1, the document key / value pairs are ordered. 2, the value of the document may be not only the inside of the string in double quotes, may also be several other types of data (or even the entire embedded document). 3, MongoDB distinguish types and sensitive. 4, MongoDB document can not have duplicate keys. 5, the key document is a string. With few exceptions, the key may be any UTF-8 characters.
Key document naming conventions: Keys do not contain \ 0 (null character). This character is used to indicate the end of the bond. . $ Have special meaning and can only be used under certain circumstances. Underscore "_" at the beginning of keys are reserved (not strictly required)
5, collection. Mongodb group is a collection of documents, similar in form RDBMAS. Collection exists in the database, a collection of no fixed structure, means that you can insert different types of formats and database, is inserted into a set of data will have some relevance. Legal collection name collection name can not be empty string "." Collection name can not contain the \ 0 character (null character), this character represents the end of the collection name. Collection name can not be "system." At the beginning, which is the prefix for the collection system reserved. User-created collection name can not contain reserved characters. Some drivers do support included in the set were there, this is because the collection contains some system-generated character. Unless you want to access this collection created by the system, or do not appear in $ name inside.
5, related commands
5.1, ######## create a database ###################
use yz #### to create a database or switch, the database is not listed in the database you just created, you need to insert some data into the database switched to db yz show dbs ##### Displays the current database admin 0.000GB config 0.000GB local 0.000 GB db.yz.insert ({ "name": "yz"}) #### to insert some data WriteResult ({ "nInserted": 1}) to the database show dbs; admin 0.000GB config 0.000GB local 0.000GB yz 0.000 GB db ############### 3 view the current database yz db.dropDatabase () ###### delete current database { "dropped": "yz", "ok" : 1 }
5.2 ################### create and delete collections (table) ##################
db.createCollection ( "user") ######## created in the current database yz user set { "ok": 1} show tables; ########### show all set user yz db.name.drop () ###### delete a collection or table name true show tables; lqb mycol1 user
5.3, ############## ################### MongoDB document manipulation using insert () or save () method to the collection insert the document, the syntax is as follows: db.COLLECTION_NAME.insert (document)
db.name.insert ({ "name": " liqingbiao"}) ######### WriteResult to insert data set name ({ "nInserted": 1 }) db.name.find () ### ########### View name the set { "_id": ObjectId ( " 5cee3d291d0f99f83952637e"), "title": "MongoDB tutorial", "description": "MongoDB is a Nosql database", "by" : "novice tutorial", "URL": " http://www.runoob.com ", "Tags": [ "MongoDB", "Database", "the NoSQL"], "Likes": {100} "the _id" : ObjectId ( "5cee3d991d0f99f83952637f"), "name": "liqingbiao"}
db.collection.update (#### update document,, {upsert:, multi:, writeConcern:})
db.name.remove({"title" : "MongoDB 教程"}) ####删除 WriteResult({ "nRemoved" : 1 }) db.name.find() { "_id" : ObjectId("5cee3d991d0f99f83952637f"), "name" : "liqingbiao" }
db.name.remove ({}) #### to delete all WriteResult ({ "nRemoved": 1}) db.name.find ()
######
db.getMongo () ####### db.stats view the current connection address database () #### to view the current status of the database db.version () ##### view the current version of the database db.createCollection ( ' phone ') ### to create a set of phone show collections showtables #### or view the collection db.log.stats () # View data state db.log.dataSize () # set of data in the original size db.log.totalIndexSize ( ) # collection index data of the original size db.log.totalSize () # set index + () # set the size of the data after compression db.log.storageSize storing the compressed size of the storage
Reproduced in: https: //blog.51cto.com/liqingbiao/2408641