MongoDB入门命令
查看所有数据库
> show dbs
admin (empty)
local 0.078GB
>
admin和管理相关, admin和local数据库不要轻易动
选择库
> use test #如果没有则创建
switched to db test
>
> db #查看当前位于哪个数据库
test
>
>
> show collections
>
db.help()请求帮助
> db.help()
DB methods:
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]
db.auth(username, password)
db.cloneDatabase(fromhost)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost)
db.createCollection(name, { size : ..., capped : ..., max : ... } )
db.createUser(userDocument)
db.currentOp() displays currently executing operations in the db
db.dropDatabase()
db.eval(func, args) run code server-side
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()
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
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.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
>
创建数据库, 没有必要去创建collections, 可以隐式创建
> use demo #没有则创建
switched to db demo
>
> db.createCollection('user')
{ "ok" : 1 }
>
> show collections
system.indexes
user
>
> show dbs
admin (empty)
demo 0.078GB
local 0.078GB
test (empty)
>
插入数据
> db.user.insert({'name': 'lisi', age: 22})
WriteResult({ "nInserted" : 1 })
>
> db.user.find()
{ "_id" : ObjectId("572f495e3c8cf279cf89ad16"), "name" : "lisi", "age" : 22 }
>
>
自动生成id, 是当做主键用的, 也可以自定义id
> db.user.insert({_id:1, name:'jack', age:19})
WriteResult({ "nInserted" : 1 })
>
> db.user.find()
{ "_id" : ObjectId("572f495e3c8cf279cf89ad16"), "name" : "lisi", "age" : 22 }
{ "_id" : 1, "name" : "jack", "age" : 19 }
>
> db.user.insert({_id:3, name:'ql', hobby:['basketball', 'football'], intro: {'title': 'my intro'}})
WriteResult({ "nInserted" : 1 })
>
> db.user.find()
{ "_id" : ObjectId("572f495e3c8cf279cf89ad16"), "name" : "lisi", "age" : 22 }
{ "_id" : 1, "name" : "jack", "age" : 19 }
{ "_id" : 3, "name" : "ql", "hobby" : [ "basketball", "football" ], "intro" : { "title" : "my intro" } }
>
直接创建collection, 不用事先声明
> db.goods.insert{name:'ql'}
WriteResult({ "nInserted" : 1 })
>
>
> show collections
goods
system.indexes
user
>
把文档赋给一个变量, 然后插入值
> doc={name:'ql', age:29}
{ "name" : "ql", "age" : 29 }
>
> db.stu.insert(doc)
WriteResult({ "nInserted" : 1 })
>
> db.stu.find()
{ "_id" : ObjectId("572f4e8b3c8cf279cf89ad18"), "name" : "ql", "age" : 29 }
>
删除collection
> show collections
goods
system.indexes
user
>
>
> db.user.drop()
true
>
> show collections
goods
system.indexes
>
删除数据库
> db
demo
>
> db.dropDatabase()
{ "dropped" : "demo", "ok" : 1 }
>
> show dbs
admin (empty)
local 0.078GB
test (empty)
>