Mongodb:nosql Database
- Important indicator points in MongoDB
- The three elements in MongoDB
- Database
- Collection
- Document
- The data store in MongoDB is stored in the form of Bson, and Bson is a binary json, so it looks like the recorded form is similar to the JSON data
- The data in the collection in MongoDB is different from the data in the relational database, the MongoDB Chinese document structure can be different, so the extensibility is very good
MongoDB Benefits: Easy-to-scale, high-performance, flexible data model
The disadvantage of MongoDB: data duplication storage, occupy large space
MongoDB Boot
Command line (terminal) Start command
- Mongod: Running the service side
- MONGO: Starting the client
View Help commands, default port, log location
- View Help: Mongod–help
- To see if it started successfully: PS ajx|grep Mongod
- Default side?: 27017
- Location of the log:/var/log/mongodb/mongod.log
- MongoDB Client
- Start the local client: MONGO
- View Help: Mongo–help
- Exit: Exit or CTRL + C
MongoDB Basic CommandsManipulating Database Commands
- View the current database: DB
- View all databases: show Dbs/show databases
- Switch database: Use db_name to delete the current database: Db.dropdatabase ()
- Switch to a database that does not have, adding data is automatically created
- Show current database status: Db.stats ()
- Current database version: Db.version ()
- View the current database link machine address: Db.getmongo ()
commands for manipulating collections
data types in the MONGO
- Object ID:? file ID
- String: Strings, most often, must be valid UTF-8
- Boolean: Store A Boolean value, True or False
- Integer: Integers can be either 32-bit or 64-bit, depending on the server
- Double: Storing floating-point values
- Arrays: An array or list in which multiple values are stored to a key
- Object: What is the file of the embedded-type?
- NULL: Store null value
- Timestamp: Timestamp, representing the total number of seconds from 1970-1-1 to present
- Date: The UNIX time format that stores the current period or time
Precautions :
- Create a statement as follows: The format of the parameter is YYYY-MM-DD each file has a property, for _id, to guarantee the uniqueness of each file
- OK?? Go to set _id plug?? File, if not provided, then MongoDB provides a unique _id for each file, type:
objectID
- Objectid is a 12-byte? Six binary number (understanding):
- The first 4 bytes are the current timestamp
- Next 3 bytes of machine ID
- The next 2 bytes in MongoDB's service process ID
- The last 3 bytes are simple increment values
Inserting Data
- Db. Collection name. Insert (document) plug?? File, if you do not specify the _id parameter, MongoDB will assign the? Objectid type of _id
- Db.stu.insert ({name: ' GJ ', gender:1})
- Db.stu.insert ({_id: "20170101", Name: ' GJ ', gender:1})
- Inserts a single specified as a dictionary, inserts multiple lines specified as a list
Save
- Db. Collection name. Save (document)
- If the _id of the file is already present, modify it if the _id of the file does not exist, then add
- Different from: Insert if there is a direct error
Simple query:
Update
- Grammar:
db.集合名称.update(<query> ,<update>,{multi: <boolean>})
- Parameter query: Query criteria
- Parameter update: update operator
- Parameter multi: optional, default is False, indicates that only the found record is updated, and a value of TRUE indicates that the full condition of the document is updated
- For example: NOTE: "Multi update only works with $ operators" Update all, you must use
$set
- Db.stu.update ({name: ' hr '},{name: ' MNC '}) updates one, and fields that are not updated are discarded.
- Db.stu.update ({name: ' hr '},{$set: {name: ' Hys '}}) update one
- Db.stu.update ({},{$set: {gender:0}},{multi:true}) Update all
Delete
- Grammar:
db.集合名称.remove(<query>,{justOne: <boolean>})
- parameter query: Optional, delete the condition of the document
- Parameter justone: Optional, if set to TRUE or 1, delete only the bar, default false, means delete more than one
MongoDB basic commands and operations