Mongodb基础操作学习

来源:互联网 发布:abb机器人编程手册 编辑:程序博客网 时间:2024/06/11 08:09
想象一下,在遥远的地方有一座大厦叫做mongodb,本来跟我们没关系,但是我们要去认识它了解它,所以我们就必须走近它走进它。

首先,在学习mongodb之前,需要知道的一件事,mongo和mongod是两个不同的概念,mongod只是一个mongodb的Server端(国人有人把它译成服务器,感觉有点儿混淆)。而mongo是一个mongodb的javascript写的shell,shell是什么?Shell就是一个解释器,你的行为用它来帮你解释给mongod听。mongo也可以叫做client端。你可以这样想象,mongod是真正核心的东西,为了保护她,mongodb官方把她建立在地底下,并且在她的上面为她建立了一栋mongo大厦,负责一切与mongod交互的事物。   

 Linux环境下,使用ps -ef | grep mongo 可以看到有一个/usr/bin/mongod 的进程,这就是她了,如果你手动kill 掉她,mongo也就无法使用了,会显示 trying reconnect to 127.0.0.1:27017, reconnect failed等提示信息,1270.0.1:27017是本地连接mongod的访问地址,27017是其使用的端口号。

想要进入mongo大厦需要念一句咒语,就像芝麻开门一样,直接命令行输入mongo,mongo的大门就这样为你敞开了。

在进入mongo这座大厦之前,有一些事情必须要知道,就好象进入大楼之前需要先告诉你怎么走?迷路了怎么办?安全出口在哪里?这不,以下这些东西就是你必须要了解的:

了解即可,整理起来方便查阅, 


help 遇到什么事了不要慌,先打911,国内事不大一般还是不要打110的好,help命令会列出一些基本的东西,大概告诉你咋处理。 
help connect connecting to a db help show dbs 显示数据库列表 
show collections 显示当前数据库中的集合(类似关系数据库中的表) 
show users 显示用户 
show profile 显示profile文件,用来分析mongdb性能的记录文档(http://blog.csdn.net/macyang/article/details/6566715) 
show logs 查看有哪些日志 
show log [name]: 查看某一个日志文件 
show dbs 列出所有的数据库 
show collections 列出某数据库下的所有集合 
show users 显示当前数据库的所有用户 大厦分为若干个操作区,一个db的,一个collection的,一个文档的,文档又有增删改查等小区。

db操作:

首先不懂就问help,来到了db就问db的help

db.help() 
DB methods: 
db.addUser(username, password[, readOnly=false]) 为当前数据库添加一个用户 
db.auth(username, password) 为当前数据库增加访问认证,就是设置个用户密码 
db.cloneDatabase(fromhost) 从fromhost地址clone数据库到本地 
db.commandHelp(name) returns the help for the command,命令帮助使用 
db.copyDatabase(fromdb, todb, fromhost) 从目标主机拷贝某数据库到本地数据库 
db.createCollection(name, { size : ..., capped : ..., max : ... } ) 创建一个集合 
db.currentOp() displays the current operation in the db db.dropDatabase() 删除当前的数据库 
db.eval(func, args) run code server-side,执行一个函数 
db.getCollection(cname) same as db['cname'] or db.cname,获取一个集合句柄,这样以后操作的话就不用每次都打
db.collectionName了 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 this connection to read from the nonmaster member of a replica pair db.getName() 当前db的name 
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.isMaster() check replica primary status 检查是否是主数据库(相对于主从数据库而言) 
db.killOp(opid) kills the current operation in the db 
db.listCommands() lists all the db commands 
db.printCollectionStats() 
db.printReplicationInfo() 
db.printSlaveReplicationInfo() 
db.printShardingStatus() 
db.removeUser(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.shutdownServer() 
db.stats() 
db.version() current version of the server 
db.getMongo().setSlaveOk() allow queries on a replication slave server 


collection collection操作有: 

db.collectionName.help() DBCollection help 
db.collectionName.find().help() - show DBCursor help 
db.collectionName.count() 统计集合下的文档数目 
db.collectionName.dataSize() 统计数据的大小 
db.collectionName.distinct( key ) - eg. db.collectionName.distinct( 'x' ) 
db.collectionName.drop() drop the collection 
db.collectionName.dropIndex(name) 删掉某索引 
db.collectionName.dropIndexes() 删掉所有索引 
db.collectionName.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups,创建索引 
db.collectionName.reIndex() 
db.collectionName.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return. e.g. db.collectionName.find( {x:77} , {name:1, x:1} ) 
db.collectionName.find(...).count() 查询符合条件的文档并统计 
db.collectionName.find(...).limit(n)查询符合条件的文档并限制前n条结果 
db.collectionName.find(...).skip(n) 查询符合条件的文档并且先跳过n条 
db.collectionName.find(...).sort(...) 查询并排序 
db.collectionName.findOne([query]) 查询一个符合条件的结果 
db.collectionName.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } ) 查询并修改 
db.collectionName.getDB() get DB object associated with collection 
db.collectionName.getIndexes() 获取所有的索引 
db.collectionName.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } ) 
db.collectionName.mapReduce( mapFunction , reduceFunction , <optional params> ) 
db.collectionName.remove(query) 
db.collectionName.renameCollection( newName , <dropTarget> ) renames the collection. 
db.collectionName.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name 
db.collectionName.save(obj) 保存一个文档对象 
db.collectionName.stats() 状态信息 db.collectionName.storageSize() - includes free space allocated to this collection 
db.collectionName.totalIndexSize() - size in bytes of all the indexes 
db.collectionName.totalSize() - storage allocated for all data and indexes 
db.collectionName.update(query, object[, upsert_bool, multi_bool]) 
db.collectionName.validate() - SLOW 
db.collectionName.getShardVersion() -only for use with sharding




待续。。。杯具,尼玛 CSDN吃屎啊

	
				
		
原创粉丝点击