Some MongoDB learning related knowledge, recorded so that the next time to view the use
Reference: https://docs.mongodb.com/manual/reference/operator/
Http://www.runoob.com/mongodb/mongodb-tutorial.html
Https://code.ziqiangxuetang.com/mongodb/mongodb-tutorial.html
1) Installation
Windows installation: Https://www.mongodb.com/download-center#community
Linux installation (Ubuntu): sudo apt-get install MongoDB
Windows environment, under the Server\3.6\bin\ directory under the installation directory there is a Mongo.exe, double-click to open
Linux environment, open the terminal input MONGO can
2) A brief description of MONGODB data structure
Combining MySQL data structure comparison
3) Data manipulation
View all databases: Show DBS
Create or switch a database: Use dbname (if the database exists then switch, do not exist to create)
Delete Current database:db. Dropdatabase()
View all tables in the database: show tables
All data operations in MongoDB use JavaScript syntax
1. Insert operation
First switch to a database, if not switch, the default in the test database operation
Of the two data, the data with ID 1, insert the operator as
Db.testable.insert ({ ' user_name ': ' Mark Hanks ', ' email ': ' [email protected] ', ' age ': +, ' City ': ' Los Angeles ' })
The insert operation, if the testable table exists, inserts the data into the table, creates the table if it does not exist, and then inserts the data
MongoDB automatically creates field _id, type Object ID
Insert document You can also use the Db.testable.save command. If you do not specify the _id field, the Save () method is similar to the Insert () method. If the _id field is specified, the data for the _id is updated.
2. Query operations
For more detailed query operation, please refer to https://docs.mongodb.com/manual/reference/operator/query/
DB. Collection. Find(query, projection)
Query is the criteria for the search, projection uses the projection operator to specify the returned key, and ignoring the argument returns all keys
To query all the data in the collection:
Db.testable.find (). Pretty ()
The find () default parameter queries all data and appends the pretty () function to format the returned document data
For example, to query the data added above, the
Db.testable.find ({ ' name ': ' Mark Hanks '})
This operation queries the data for the last Name field, Mark Hanks
To query for data older than 20
Db.testable.find ({' Age ': {' $gt ': 20}})
The above operation looks for data older than 20 GT corresponds to English greater than
The corresponding comparison symbols are:
$GT: Greater than (greater than)
$gte: Greater than or equal (greater than equal)
$LT: Smaller than (less than)
$lte: Less than or equal (than equal)
If you want to compare multiple times, you can separate them with commas, such as querying documents larger than 20 less than 25
Db.testable.find ({' Age ': {' $gt ': $, ' $lt ': 25}})
You can also compare data types, such as querying the Name field for a string type of data
Db.testable.find ({' name ': {' $type ': 2}})
2 is the type number of the string, and the corresponding code
The skip function and the limit function are useful if you want to implement how many data to skip and how many data to use.
For example, to skip the first 5 data and remove 5 data, then:
Db. Collection_name.find (). Limit (5). Skip (5)
Limit limits the number of bars to take out data, skip limits the number of skipped data
3. Delete operation
The basic syntax format for the Remove () method
Db.collection.remove ( <query>, <justOne>)
The syntax format after MongoDB version 2.6
Db.collection.remove ( <query>, { justone: <boolean>, Writeconcern: <document> })
which
-
- query : (optional) The condition of the deleted document.
- justone : (optional) If set to TRUE or 1, only one document is deleted.
- Writeconcern : (optional) the level at which an exception is thrown
For example, to delete the data inserted above, you can perform an action
Db.testable.remove ({ {' name ': ' Mark Hanks '}, {justone:true}})
This means deleting a data with the name Mark Hanks and, if Justone is false, deleting all data with the name Mark Hanks, which defaults to False
If you empty all the data in the collection, execute Db.testable.remove ({}) to
4) Update operation
The syntax format is as follows:
Db.collection.update ( <query>, <update>, { upsert: <boolean>, Multi: < Boolean>, writeconcern: <document> })
-
- Query: The search condition for update, similar to where in SQL update query.
- Update: Update object and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
- upsert : Optional, this parameter means that if there is no record of update, insert objnew,true as INSERT, default is False, do not insert.
- multi : Optional, mongodb default is False, only update the first record found, if this parameter is true, will be found on the condition of a number of records update all.
- writeconcern : Optional, throws an exception level.
For example, to set the age of the data user_name to mark Hanks to 30
Db.collection.update ( {' user_name ': ' Mark Hanks '}, {' $set ': {' age ': +}}, { multi:true })
For more detailed update operations please refer to https://docs.mongodb.com/manual/reference/operator/update/
MongoDB usage Record