close mongodb command
use admin #Switch to admin user
- db.shutdownServer() #shutdown mongodb
pkill mongod
show all databases
- show dbs
switch database
- use database name
delete database
- db.dropDatabase()
view all collections
- show collections
view all tables
- show tables
data writing
- db.collection_name.insert({x:1,y:2})
- db.collection name (such as mysql table name).insert (written json format data)
data query
- db.collection_name.find()
- query all data
- db.collection_name.find({x:1})
- Query data where x is equal to 1
- db.collection_name.find().count()
- Quantity that satisfies the condition
- db.collection_name.find().skip(3).limit(2).sort({x:1})
- skip: such as the offset of mysql
- limit: get the number of data
- sort: sort, 1 positive order, -1 reverse order
data update
- db.tab1.update({x:1},{x:999})
- Update the first piece of data with x equal to 1 to 999, only the first piece of data that satisfies the condition will be updated (only one piece of data will be updated)
- db.tab1.update({x:1},{$set:{x:999,y:888}})
- Existing fields will only update the value, non-existing fields will be incremented
- db.tab1.update({x:1},{x:999},true)
- Automatically write when updating non-existent data
- db.tab1.update({x:1},{$set:{x:999}},false,true)
- Update all data that meets the condition
deletion of data
- db.tab1.remove({x:1})
- delete data where x is equal to 1
mongodb create user
- Creation syntax: createUser (addUser before 2.6)
- db.createUser({user:”listen”,pwd:”listen”,roles:[{role:”userAdmin”,db:”demo”}]})