美文网首页技术栈
2019-05-27——MongoDB 基本命令

2019-05-27——MongoDB 基本命令

作者: 烟雨乱平生 | 来源:发表于2019-05-27 23:23 被阅读0次

MongoDB命令帮助系统

链接mongoDB后可以输入help查看帮助系统

> help
    db.help()                    help on db methods
    db.mycoll.help()             help on collection methods
    sh.help()                    sharding helpers
    rs.help()                    replica set helpers
    help admin                   administrative help
    help connect                 connecting to a db help
    help keys                    key shortcuts
    help misc                    misc things to know
    help mr                      mapreduce

    show dbs                     show database names
    show collections             show collections in current database
    show users                   show users in current database
    show profile                 show most recent system.profile entries with time >= 1ms
    show logs                    show the accessible logger names
    show log [name]              prints out the last segment of log in memory, 'global' is default
    use <db_name>                set current database
    db.foo.find()                list objects in collection foo
    db.foo.find( { a : 1 } )     list objects in foo where a == 1
    it                           result of the last line evaluated; use to further iterate
    DBQuery.shellBatchSize = x   set default number of items to display on shell
    exit                         quit the mongo shell

这是MongoDB最顶层的命令列表,主要告诉我们管理数据库相关的一些抽象的范畴:数据库操作帮助、集合操作帮助、管理帮助。

如果你想了解数据库操作更详细的帮助命令,可以直接使用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() - 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

对数据库进行管理和操作的基本命令,可以从上面获取到。如果想要得到更多,而且每个命令的详细用法,可以使用上面列出的db.listCommands()查询。

另一个比较基础的是对指定数据库的集合进行操作、管理和监控,可以通过查询db.mycoll.help()获取到:

> db.mycoll.help()
DBCollection help
    db.mycoll.find().help() - show DBCursor help
    db.mycoll.bulkWrite( operations, <optional params> ) - bulk execute write operations, optional parameters are: w, wtimeout, j
    db.mycoll.count( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS
    db.mycoll.copyTo(newColl) - duplicates collection by copying all documents to newColl; no indexes are copied.
    db.mycoll.convertToCapped(maxBytes) - calls {convertToCapped:'mycoll', size:maxBytes}} command
    db.mycoll.createIndex(keypattern[,options])
    db.mycoll.createIndexes([keypatterns], <options>)
    db.mycoll.dataSize()
    db.mycoll.deleteOne( filter, <optional params> ) - delete first matching document, optional parameters are: w, wtimeout, j
    db.mycoll.deleteMany( filter, <optional params> ) - delete all matching documents, optional parameters are: w, wtimeout, j
    db.mycoll.distinct( key, query, <optional params> ) - e.g. db.mycoll.distinct( 'x' ), optional parameters are: maxTimeMS
    db.mycoll.drop() drop the collection
    db.mycoll.dropIndex(index) - e.g. db.mycoll.dropIndex( "indexName" ) or db.mycoll.dropIndex( { "indexKey" : 1 } )
    db.mycoll.dropIndexes()
    db.mycoll.ensureIndex(keypattern[,options]) - DEPRECATED, use createIndex() instead
    db.mycoll.explain().help() - show explain help
    db.mycoll.reIndex()
    db.mycoll.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
                                                  e.g. db.mycoll.find( {x:77} , {name:1, x:1} )
    db.mycoll.find(...).count()
    db.mycoll.find(...).limit(n)
    db.mycoll.find(...).skip(n)
    db.mycoll.find(...).sort(...)
    db.mycoll.findOne([query], [fields], [options], [readConcern])
    db.mycoll.findOneAndDelete( filter, <optional params> ) - delete first matching document, optional parameters are: projection, sort, maxTimeMS
    db.mycoll.findOneAndReplace( filter, replacement, <optional params> ) - replace first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument
    db.mycoll.findOneAndUpdate( filter, update, <optional params> ) - update first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument
    db.mycoll.getDB() get DB object associated with collection
    db.mycoll.getPlanCache() get query plan cache associated with collection
    db.mycoll.getIndexes()
    db.mycoll.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
    db.mycoll.insert(obj)
    db.mycoll.insertOne( obj, <optional params> ) - insert a document, optional parameters are: w, wtimeout, j
    db.mycoll.insertMany( [objects], <optional params> ) - insert multiple documents, optional parameters are: w, wtimeout, j
    db.mycoll.mapReduce( mapFunction , reduceFunction , <optional params> )
    db.mycoll.aggregate( [pipeline], <optional params> ) - performs an aggregation on a collection; returns a cursor
    db.mycoll.remove(query)
    db.mycoll.replaceOne( filter, replacement, <optional params> ) - replace the first matching document, optional parameters are: upsert, w, wtimeout, j
    db.mycoll.renameCollection( newName , <dropTarget> ) renames the collection.
    db.mycoll.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
    db.mycoll.save(obj)
    db.mycoll.stats({scale: N, indexDetails: true/false, indexDetailsKey: <index key>, indexDetailsName: <index name>})
    db.mycoll.storageSize() - includes free space allocated to this collection
    db.mycoll.totalIndexSize() - size in bytes of all the indexes
    db.mycoll.totalSize() - storage allocated for all data and indexes
    db.mycoll.update( query, object[, upsert_bool, multi_bool] ) - instead of two flags, you can pass an object with fields: upsert, multi
    db.mycoll.updateOne( filter, update, <optional params> ) - update the first matching document, optional parameters are: upsert, w, wtimeout, j
    db.mycoll.updateMany( filter, update, <optional params> ) - update all matching documents, optional parameters are: upsert, w, wtimeout, j
    db.mycoll.validate( <full> ) - SLOW
    db.mycoll.getShardVersion() - only for use with sharding
    db.mycoll.getShardDistribution() - prints statistics about data distribution in the cluster
    db.mycoll.getSplitKeysForChunks( <maxChunkSize> ) - calculates split points over all chunks and returns splitter function
    db.mycoll.getWriteConcern() - returns the write concern used for any operations on this collection, inherited from server/db if set
    db.mycoll.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the collection
    db.mycoll.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the collection

相关文章

  • 2019-05-27——MongoDB 基本命令

    MongoDB命令帮助系统 链接mongoDB后可以输入help查看帮助系统 这是MongoDB最顶层的命令列表,...

  • MongoDB基本命令

    MongoDB基本命令 help命令: db.help()命令:

  • MongoDB

    MongoDB基本概念: MongoDB服务器端启动: MongoDB客户端启动: CURD常用命令:

  • MongoDB--周国康笔记

    1. MongoDB命令帮助系统 2. 基本命令及实例 一基本命令 二基本DDL和DML 三启动与终止 四安全管理...

  • MongoDB 基本命令

  • mongoDB基本命令

    一、mongoDb安装 在mac上安装了brew的情况下,可以直接执行命令 brew install mongod...

  • MongoDB基本操作命令

    MongoDB数据类型 MongoDB创建/删除数据库 use DATABASE_NAME 如果数据库不存在,创...

  • MongoDB(基本命令)

    查看数据库 转到what_i_love数据库(如果没有则自动创建一个) 查看当前数据库的表 删除当前数据库 查看u...

  • MongoDB 基本命令

    一、数据库常用命令1、Help查看命令提示 复制代码 代码如下: helpdb.help();db.yourCol...

  • MongoDB基本命令

    为什么MongoDB对前端开发人员友好,因为它的语法和MongoDB太相似了 图中的var定义变量,是不是和 Ja...

网友评论

    本文标题:2019-05-27——MongoDB 基本命令

    本文链接:https://www.haomeiwen.com/subject/xrrltctx.html