1.MongoDB安装

  安装包下载地址: https://www.mongodb.com/download-center/community

  启动数据库:进入到mongd所在的bin目录,执行mongod --dbpath d:\mongodb\data         (d:\mongodb\data为数据将要保存的文件夹路径)

  连接数据库:将D:\MongoDB\mongodb-win32-i386-2.6.9\bin配置到环境变量,在cmd命令行输入 mongo 127.0.0.1:27017

2. MongoDB常用命令

  show dbs  显示所有数据库

  use dbname  使用名字为dbname的数据库,若数据库dbname不存在则创建

  db   显示当前使用的数据库名字(或者db.getName())

  db.dropDatabase()    删除数据库

  show collections 查看当前数据库的集合(collection,即表格)

  db.collection.drop() 删除集合

use test                                                           #使用数据库test,不存在时创建
db.test.user.insert({"name":"zack","age":23})    #在test库的user集合中插入一条数据记录,user不存在时创建集合user


帮助命令
1. db.help()

 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

2. db.test.help()        对数据库test的所有操作命令:

 db.test.find().help() - show DBCursor help
 db.test.count()
 db.test.copyTo(newColl) - duplicates collection by copying all documents to newColl; no indexes are copied.
 db.test.convertToCapped(maxBytes) - calls {convertToCapped:'test', size:maxBytes}} command
 db.test.dataSize()
 db.test.distinct( key ) - e.g. db.test.distinct( 'x' )
 db.test.drop() drop the collection
 db.test.dropIndex(index) - e.g. db.test.dropIndex( "indexName" ) or db.test.dropIndex( { "indexKey" : 1 } )
 db.test.dropIndexes()
 db.test.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
 db.test.reIndex()
 db.test.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
                                               e.g. db.test.find( {x:77} , {name:1, x:1} )
 db.test.find(...).count()
 db.test.find(...).limit(n)
 db.test.find(...).skip(n)
 db.test.find(...).sort(...)
 db.test.findOne([query])
 db.test.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
 db.test.getDB() get DB object associated with collection
 db.test.getPlanCache() get query plan cache associated with collection
 db.test.getIndexes()
 db.test.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
 db.test.insert(obj)
 db.test.mapReduce( mapFunction , reduceFunction , <optional params> )
 db.test.aggregate( [pipeline], <optional params> ) - performs an aggregation on a collection; returns a cursor
 db.test.remove(query)
 db.test.renameCollection( newName , <dropTarget> ) renames the collection.
 db.test.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
 db.test.save(obj)
 db.test.stats()
 db.test.storageSize() - includes free space allocated to this collection
 db.test.totalIndexSize() - size in bytes of all the indexes
 db.test.totalSize() - storage allocated for all data and indexes
 db.test.update(query, object[, upsert_bool, multi_bool]) - instead of two flags, you can pass an object with fields: upsert, multi
 db.test.validate( <full> ) - SLOW
 db.test.getShardVersion() - only for use with sharding
 db.test.getShardDistribution() - prints statistics about data distribution in the cluster
 db.test.getSplitKeysForChunks( <maxChunkSize> ) - calculates split points over all chunks and returns splitter function
 db.test.getWriteConcern() - returns the write concern used for any operations on this collection, inherited from server/db if set
 db.test.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the collection


3. db.test.user.help()  对集合user的所有操作命令:

db.test.user.find().help() - show DBCursor help
db.test.user.count()
db.test.user.copyTo(newColl) - duplicates collection by copying all documents to newColl; no indexes are copied.
db.test.user.convertToCapped(maxBytes) - calls {convertToCapped:'test.user', size:maxBytes}} command
db.test.user.dataSize()
db.test.user.distinct( key ) - e.g. db.test.user.distinct( 'x' )
db.test.user.drop() drop the collection
db.test.user.dropIndex(index) - e.g. db.test.user.dropIndex( "indexName" ) or db.test.user.dropIndex( { "indexKey" : 1 } )
db.test.user.dropIndexes()
db.test.user.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
db.test.user.reIndex()
db.test.user.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
                                              e.g. db.test.user.find( {x:77} , {name:1, x:1} )
db.test.user.find(...).count()
db.test.user.find(...).limit(n)
db.test.user.find(...).skip(n)
db.test.user.find(...).sort(...)
db.test.user.findOne([query])
db.test.user.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
db.test.user.getDB() get DB object associated with collection
db.test.user.getPlanCache() get query plan cache associated with collection
db.test.user.getIndexes()
db.test.user.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
db.test.user.insert(obj)
db.test.user.mapReduce( mapFunction , reduceFunction , <optional params> )
db.test.user.aggregate( [pipeline], <optional params> ) - performs an aggregation on a collection; returns a cursor
db.test.user.remove(query)
db.test.user.renameCollection( newName , <dropTarget> ) renames the collection.
db.test.user.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
db.test.user.save(obj)
常用命令

相关文章:

  • 2021-09-09
  • 2021-12-10
  • 2021-07-27
  • 2021-08-13
  • 2021-06-23
  • 2021-05-25
  • 2022-12-23
猜你喜欢
  • 2021-10-17
  • 2022-01-01
  • 2022-01-04
  • 2021-12-08
  • 2021-07-04
  • 2022-01-21
相关资源
相似解决方案