数据库操作 |
Mongodb |
MySQL |
查询库 |
show databases | show dbs |
show databases |
选中库 |
use databaseName |
use databaseName |
查询表 |
show tables | show collections |
show tables |
创建表 |
db.createCollection(‘collectionName’) |
create table tableName... |
删除表 |
db.collectionName.drop |
drop table tableName |
删除库 |
db.dropDatabase |
drop database databaseName |
添加表数据 |
db.collectionName.insert |
insert |
查询表数据 |
db.collectionName.find |
select |
修改表数据 |
db.collectionName.update |
update |
删除表数据 |
db.collectionName.remove |
delete |
如上述所示,mongodb同样兼容了部分传统数据库的命令,其中有必要说一下的是mongodb中创建库采用的是隐式创建,即在use 一个不存在的库时就会变为创建库,use databaseName 有则选中无则创建,但这里还没有创建完毕,需要进一步创建表才算创建完毕;同时创建表时也允许隐式创建,即db.collectionName.insert 往一个不存在的表中添加数据就会先创建表再添加数据
一:常用命令
1.1:查看当前的数据库
> show dbs admin 0.000GB config 0.000GB local 0.000GB shop 0.000GB > show databases admin 0.000GB config 0.000GB local 0.000GB shop 0.000GB
1.2:选中库
> use shop switched to db shop
1.3:查看表
> show collections user > show tables user
1.4.1:创建库/表
> use good switched to db good > db.createCollection('items') { "ok" : 1 }
1.4.2:创建库/表第二种方式
> use cate switched to db cate > db.cateList.insert({"_id":1,"cateName":"FAST"}) WriteResult({ "nInserted" : 1 })
1.4.3:在创建库时如果没有创建表,库也会创建失败
> use cate switched to db cate > show dbs admin 0.000GB config 0.000GB good 0.000GB local 0.000GB shop 0.000GB
1.5:删除表
> db.cateList.drop() true
1.6:删除库
> use good switched to db good > db.dropDatabase() { "dropped" : "good", "ok" : 1 }
1.7:使用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.eva l() - 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() g