Mongodbubuntu
sudo apt-get install MongoDB
Mac
Brew Install MongoDB
Start
Direct start
sudo mongod
Caveats: Startup failure
Database does not exist create folder/data/db
Insufficient permissions to addsudo
Start parameter description
--port
To set the default port
--dbpath
Setting the database path
--config
By specifying the configuration file to start, the configuration mongodb
file can be configured by default port, database path and so on
Shut down
Direct kill off (very deprecated, preferably not used)
sudo kill-9 process number
Close directly by entering the database
Use Admindb.shutdownserver ()
Database Display Database list
Show databases//or show DBS
Show current Database
Db
Enter the database
Use database name
Create a database
Create a database automatically whenever you insert data
Deleting a database
Premise use db//Delete database Db.dropdatabase ()
Collection (table) operations Create a collection
The collection is automatically created by default whenever you insert data
Create a collection manually
Db.createcollection (collection name)
Show List of collections
Show collections
Delete Collection
Db. Collection name. Drop ()
Data manipulation creation
Insert data, JSON string Db.my_coll.insert ({"A": "X", "B": {"Q": "A", "P": "B"}, "C": [X-ray]})//BULK INSERT data = [ {"test01": "Val1"}, {"test02": "Val2"}, {"A": {"P": "Q"}, "B": 123}]db.my_coll.insertmany (data)
Update
Default overall update, if you need to use local update, the $set
default is to update only once, if you need to add a third parameter batch updatemulti
Default update is a full update, that is, replace db.stu.update ( // update Condition { "name": "Guo Jing" }, // update content { "age": 88 })// Partial update db.stu.update ( // update conditions { "name": "Huang Rong" }, // update content { // locally updated content $set:{ "age":80 } })// is updated by default only once, and if a batch update is required, add a third parameter, multi set to true is the bulk update db.stu.update ( // update Condition { "Age": {$gt:20} }, // update content { // locally updated content $set:{ "age":36 } }, // How to update { multi:true })
Delete
Delete Db.stu.remove (//Delete condition {"Age": 36},//If you want to delete only one {justone:true})//clear the data Db.stu.remov E ({})
Save
Query data via _id If there is an update, insert Db.my_test.save If it does not exist ({"_id": 1, "a": "B"})
Query Simple Query
Db. Collection name. Find ()
Complex queries
Default query is and Db.stu.find ( // query criteria { "Hometown": "Mongolia", "age": 20 })// comparison operator ($GT, $gte, $lt, $lte, $ne) db.stu.find ( // query condition { "Age": {$gt:20} })// logical operators $ORDB. Stu.find ( // query criteria { "Age": {$gt:20}, $or: [ {"Hometown": "Mongolia"}, {"Gender":false} ] })// range operator $in, $nin// represents db.stu.find in the list ( // Query Conditions { "Age": {$nin: [18,45]} }]/ / Regular Expression Db.stu.find ( // query condition { "name":/^ yellow/ }) Db.stu.find ( // query criteria { "name": {$regex: "^ yellow"} })// custom query Db.stu.find ( // query criteria { $where: function () { // return true Show qualifying // this indicates the current record return this.age >= 18 && this.gender == false } })
Advanced Query
Limit and skip//Note: Skip before limit no matter who first who db.stu.find (). Limit (2) db.stu.find (). Skip (1) db.stu.find (). Limit (2). Skip (1) Db.stu.find (). Skip (1). Limit (2)//Whether the projection shows data//in different database versions in different ways Db.stu.find (//Query condition {},//projection method, display data field {" Name ": 1})//Sort///parameter 1 for ascending order//parameter-1 for descending order Db.stu.find (). Sort ({age:-1, hometown:1})//Count Db.stu.find (). Count ()//Remove duplicates//The first parameter is a deduplication field//The second is a query condition db.stu.distinct (' hometown ', {age:{$gt: 18}})
MongoDB Simple operation