zoukankan      html  css  js  c++  java
  • 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)
    > 
    
    
  • 相关阅读:
    年终总结 2016-08-28 22:04 422人阅读 评论(26) 收藏
    [mysql]MySQL Daemon failed to start 2016-08-14 21:27 1121人阅读 评论(18) 收藏
    solrr初步了解 2016-07-31 22:29 380人阅读 评论(4) 收藏
    基于spring-boot的测试桩设计--几种常见的controller
    利用Factory-boy和sqlalchemy来批量生成数据库表数据
    job中shell脚本异常(删除不存在容器),导致job被打断执行的问题 脚本优化方法
    利用Factory-boy来生成实例数据
    pytest相关资源收集
    pytest 用 @pytest.mark.usefixtures("fixtureName")装饰类,可以让执行每个case前,都执行一遍指定的fixture
    pytest fixture 利用 params参数实现用例集合
  • 原文地址:https://www.cnblogs.com/qlshine/p/5950560.html
Copyright © 2011-2022 走看看