1.mongodb command entry
1.1 show databases; or show dbs; // view the current database
> show dbs; admin 0.000GB config 0.000GB local 0.000GB
1.2 use databaseName selected library
show tables / collections View collections in the current library
1.3 How to Create a Library
mongodb library is implicitly created, you can use a library that does not exist
Then create a collection in the library, you can create a library
1.4 db.createCollection('collectionName'); //创建collection
1.5 collection allows implicit creation
db.collectionName.insert(document);
1.6 db.collectionName.drop(); /删除collection
> use shop switched to db shop > db.createCollection('user'); { "ok" : 1 } > show dbs; admin 0.000GB config 0.000GB local 0.000GB shop 0.000GB > show collections; user
1.7 inserted user table statement
(1) id value generated automatically
db.user.insert({name:'lisi',age:22}) WriteResult({ "nInserted" : 1 }) > db.user.find(); { "_id" : ObjectId("5d73077c71b815674de4d152"), "name" : "lisi", "age" : 22 }
(2) generating the specified id
> db.user.insert({_id:2,name:'wangwu',age:25}) WriteResult({ "nInserted" : 1 }) > db.user.find(); { "_id" : ObjectId("5d73077c71b815674de4d152"), "name" : "lisi", "age" : 22 } { "_id" : 2, "name" : "wangwu", "age" : 25 }
(3) Insert a multilayer
> db.user.insert({_id:3,name:'xiaobing',hobby:['basketball','football'],intro:{'title':'My intro','content':'from china'}}); WriteResult({ "nInserted" : 1 }) > db.user.find(); { "_id" : ObjectId("5d73077c71b815674de4d152"), "name" : "lisi", "age" : 22 } { "_id" : 2, "name" : "wangwu", "age" : 25 } { "_id" : 3, "name" : "xiaobing", "hobby" : [ "basketball", "football" ], "intro" : { "title" : "My intro", "content" : "from china" } } >
In fact, need not be declared 1.8 mongodb table, table data can be written directly, you can create success!
> show tables; user > db.goods.insert({_id:1,name:'oppoR11',price:'3000'}); WriteResult({ "nInserted" : 1 }) > show tables; goods user > db.goods.find() { "_id" : 1, "name" : "oppoR11", "price" : "3000" }
1.9 Delete table db.collectionName.drop ();
> show collections goods user > db.goods.drop(); true > show collections; user >
2.0 Delete Database
db.dropDatabase()
> show dbs; admin 0.000GB config 0.000GB local 0.000GB shop 0.000GB > use shop; switched to db shop > db.dropDatabase(); { "dropped" : "shop", "ok" : 1 } > show dbs; admin 0.000GB config 0.000GB local 0.000GB >
2.1 Search Help db.help ();
> db.help() DB methods: db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)] db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor db.auth(username, password) db.cloneDatabase(fromhost) - deprecated db.commandHelp(name) returns the help for the command db.copyDatabase(fromdb, todb, fromhost) - deprecated db.createCollection(name, {size: ..., capped: ..., max: ...}) db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions}) db.createUser(userDocument) db.currentOp() displays currently executing operations in the db db.dropDatabase() db.eval() - deprecated db.fsyncLock() flush data to disk and lock server for backups db.fsyncUnlock() unlocks server following a db.fsyncLock() db.getCollection(cname) same as db['cname'] or db.cname db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections db.getCollectionNames() db.getLastError() - just returns the err msg string db.getLastErrorObj() - return full status object db.getLogComponents() db.getMongo() get the server connection object db.getMongo().setSlaveOk() allow queries on a replication slave server db.getName() db.getPrevError() db.getProfilingLevel() - deprecated db.getProfilingStatus() - returns if profiling is on and slow threshold db.getReplicationInfo() db.getSiblingDB(name) get the db at the same server as this one db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set db.hostInfo() get details about the server's host db.isMaster() check replica primary status db.killOp(opid) kills the current operation in the db db.listCommands() lists all the db commands db.loadServerScripts() loads all the scripts in db.system.js db.logout() db.printCollectionStats() db.printReplicationInfo() db.printShardingStatus() db.printSlaveReplicationInfo() db.dropUser(username) db.repairDatabase() db.resetError() db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj: 1} db.serverStatus() db.setLogLevel(level,<component>) db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db db.setVerboseShell(flag) display extra information in shell output db.shutdownServer() db.stats() db.version() current version of the server
To be follow-up supplement ....