(转载)MongoingDB常用操作
mongo –path
db.AddUser(username,password) 添加用户
db.auth(usrename,password) 设置数据库连接验证
db.cloneDataBase(fromhost) 从目标服务器克隆一个数据库
db.commandHelp(name) returns thehelpforthe command
db.copyDatabase(fromdb,todb,fromhost) 复制数据库fromdb—源数据库名称,todb—目标数据库名称,fromhost—源数据库服务器地址
db.createCollection(name,{size:3333,capped:333,max:88888}) 创建一个数据集,相当于一个表
db.currentOp() 取消当前库的当前操作
db.dropDataBase() 删除当前数据库
db.eval(func,args) run code server-side
db.getCollection(cname) 取得一个数据集合,同用法:db['cname']ordb.cname
db.getCollenctionNames() 取得所有数据集合的名称列表
db.getLastError() 返回最后一个错误的提示消息
db.getLastErrorObj() 返回最后一个错误的对象
db.getMongo() 取得当前服务器的连接对象get the server connectionobject
db.getMondo().setSlaveOk() allow this connection to readfromthen nonmaster membr of a replica pair
db.getName() 返回当操作数据库的名称
db.getPrevError() 返回上一个错误对象
db.getProfilingLevel() ?什么等级
db.getReplicationInfo() ?什么信息
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(杀死)在当前库的当前操作
db.printCollectionStats() 返回当前库的数据集状态
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回当前数据库是否为共享数据库
db.removeUser(username) 删除用户
db.repairDatabase() 修复当前数据库
db.resetError()
db.runCommand(cmdObj) run a database command.ifcmdObj is a string, turns it into {cmdObj:1}
db.setProfilingLevel(level) 0=off,1=slow,2=all
db.shutdownServer() 关闭当前服务程序
db.version() 返回当前程序的版本信息
数据集(表)操作语法
db.linlin.find({id:10}) 返回linlin数据集ID=10的数据集
db.linlin.find({id:10}).count() 返回linlin数据集ID=10的数据总数
db.linlin.find({id:10}).limit(2)返回linlin数据集ID=10的数据集从第二条开始的数据集
db.linlin.find({id:10}).skip(8) 返回linlin数据集ID=10的数据集从0到第八条的数据集
db.linlin.find({id:10}).limit(2).skip(8) 返回linlin数据集ID=1=的数据集从第二条到第八条的数据
db.linlin.find({id:10}).sort() 返回linlin数据集ID=10的排序数据集
db.linlin.findOne([query]) 返回符合条件的一条数据
db.linlin.getDB() 返回此数据集所属的数据库名称
db.linlin.getIndexes() 返回些数据集的索引信息
db.linlin.group({key:…,initial:…,reduce:…[,cond:...]})
db.linlin.mapReduce(mayFunction,reduceFunction,)
db.linlin.remove(query) 在数据集中删除一条数据
db.linlin.renameCollection(newName) 重命名些数据集名称
db.linlin.save(obj) 往数据集中插入一条数据
db.linlin.stats() 返回此数据集的状态
db.linlin.storageSize() 返回此数据集的存储大小
db.linlin.totalIndexSize() 返回此数据集的索引文件大小
db.linlin.totalSize() 返回些数据集的总大小
db.linlin.update(query,object[,upsert_bool])在此数据集中更新一条数据
db.linlin.validate() 验证此数据集
db.linlin.getShardVersion() 返回数据集共享版本号
db.linlin.find({‘name’:'foobar’}) select*fromlinlin where name=’foobar’
db.linlin.find() select*fromlinlin
db.linlin.find({‘ID’:10}).count() select count(*)fromlinlin whereID=10
db.linlin.find().skip(10).limit(20) 从查询结果的第十条开始读20条数据 select * from linlin limit 10,20 ———-mysql
db.linlin.find({‘ID’:{$in:[25,35,45]}}) select * from linlin where IDin(25,35,45)
db.linlin.find().sort({‘ID’:-1}) select * from linlin order by IDdesc
db.linlin.distinct(‘name’,{‘ID’:{$lt:20}}) select distinct(name)fromlinlin whereID<</code>20
db.linlin.group({key:{‘name’:true},cond:{‘name’:'foo’},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})
select name,sum(marks)fromlinlin group by name
db.linlin.find(‘this.ID<</code>20′,{name:1}) select name from linlin where ID<</code>20
db.linlin.insert({‘name’:'foobar’,'age’:25}) insert into linlin (‘name’,'age’)values(‘foobar’,25)
db.linlin.insert({‘name’:'foobar’,'age’:25,’email’:'cclove2@163.com’})
db.linlin.remove({}) delete*fromlinlin
db.linlin.remove({‘age’:20}) delete linlin where age=20
db.linlin.remove({‘age’:{$lt:20}}) delete linlin where age<</code>20
db.linlin.remove({‘age’:{$lte:20}}) delete linlin where age<</code>=20
db.linlin.remove({‘age’:{$gt:20}}) delete linlin where age>20
db.linlin.remove({‘age’:{$gte:20}}) delete linlin where age>=20
db.linlin.remove({‘age’:{$ne:20}}) delete linlin where age!=20
db.linlin.update({‘name’:'foobar’},{$set:{‘age’:36}}) update linlinsetage=36where name=’foobar’
db.linlin.update({‘name’:'foobar’},{$inc:{‘age’:3}}) update linlinsetage=age+3where name=’foobar’
超级用户相关:
use admin
#增加或修改用户密码
db.addUser(ixigua,'pwd')
#查看用户列表
db.system.users.find()
#用户认证
db.auth(ixigua,'pwd')
#删除用户
db.removeUser('mongodb')
#查看所有用户
show users
#查看所有数据库
show dbs
#查看所有的collection
show collections
#查看各collection的状态
db.printCollectionStats()
#查看主从复制状态
db.printReplicationInfo()
#修复数据库
db.repairDatabase()
#设置记录profiling,0=off 1=slow 2=all
db.setProfilingLevel(1)
#查看profiling
show profile
#拷贝数据库
db.copyDatabase('mail_addr','mail_addr_tmp')
#删除collection
db.mail_addr.drop()
#删除当前的数据库
db.dropDatabase()
客户端连接
/usr/local/mongodb/bin/mongo 8.8.88/ixigualib -u ixigua -p 'pwd'
增删改
#存储嵌套的对象
db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]})
#存储数组对象
db.user_addr.save({'Uid':'yushunzhi@sohu.com','Al':['test-1@sohu.com','test-2@sohu.com']})
#根据query条件修改,如果不存在则插入,允许修改多条记录
db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true)
#删除yy=5的记录
db.foo.remove({'yy':5})
#删除所有的记录
db.foo.remove()
索引
#增加索引:1(ascending),-1(descending)
db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
#索引子对象
db.user_addr.ensureIndex({'Al.Em': 1})
#查看索引信息
db.deliver_status.getIndexes()
db.deliver_status.getIndexKeys()
#根据索引名删除索引
db.user_addr.dropIndex('Al.Em_1')
查询
#查找所有
db.foo.find()
#查找一条记录
db.foo.findOne()
#根据条件检索10条记录
db.foo.find({'msg':'Hello 1'}).limit(10)
#sort排序
db.deliver_status.find({'From':'ixigua@sina.com'}).sort({'Dt',-1})
db.deliver_status.find().sort({'Ct':-1}).limit(1)
#count操作
db.user_addr.count()
#distinct操作
db.foo.distinct('msg')
#>操作
db.foo.find({"timestamp": {"$gte" : 2}})
#子对象的查找
db.foo.find({'address.city':'beijing'})
管理
#查看collection数据的大小
db.deliver_status.dataSize()
#查看colleciont状态
db.deliver_status.stats()
#查询所有索引的大小
db.deliver_status.totalIndexSize()
http://blog.sina.com.cn/s/blog_657f548401016wev.html
(转载)MongoingDB常用操作的更多相关文章
- (转载)StringGrid常用属性和常用操作
Delphi StringGrid常用属性和常用操作 StringGrid组件用于建立显示字符串的网格,与电子表格相似.它可使表格中的字符串和相关对象操作简单化.StringGrid组件提供了许多可控 ...
- (转载)Delphi StringGrid常用属性和常用操作
Delphi StringGrid常用属性和常用操作 StringGrid组件用于建立显示字符串的网格,与电子表格相似.它可使表格中的字符串和相关对象操作简单化.StringGrid组件提供了许多可控 ...
- 转载:R语言Data Frame数据框常用操作
Data Frame一般被翻译为数据框,感觉就像是R中的表,由行和列组成,与Matrix不同的是,每个列可以是不同的数据类型,而Matrix是必须相同的. Data Frame每一列有列名,每一行也可 ...
- NSString&NSMutableString常用操作梳理(转)
作者:弦苦 授权本站转载. 上一篇梳理了NSArray&NSMutableArray常用操作,这次来梳理一下Objective-C中每天都要用到的字符串处理类——NSString. Objec ...
- 关于vim的常用操作
vim常用操作和使用技巧 vi是linux与unix下的常用文本编辑器,其运行稳定,使用方便,本文将分两部分对其常用操作技巧和配置进行阐述,其中参考了网上的一些文章,对作者表示感谢 PART1 操作技 ...
- vim常用操作和使用技巧
vi是linux与unix下的常用文本编辑器,其运行稳定,使用方便,本文将分两部分对其常用操作技巧和配置进行阐述,其中参考了网上的一些文章,对作者表示感谢 PART1 操作技巧 说明: 以下的例子中 ...
- UITableView取消选中颜色、常用操作
UITableView取消选中颜色.常用操作 使用空白view取代cell - (UITableViewCell *)tableView:(UITableView *)tableView cell ...
- 我的Android进阶之旅------>温习Sqlite3的常用操作
前言;今天要写一个应用来调节系统的Brightness值,来改变系统的背光亮度.由于刚开始些的时候没有考虑Brightness的最小值,直接托动SeekBar到最小值(为0).瞬间,屏幕变成全黑,失败 ...
- python anaconda 常用操作;conda 命令指南
在使用 python anaconda时,经常会用到很多常用操作,记录下来,方便以后更好地使用: conda: Conda既是一个包管理器又是一个环境管理器.你肯定知道包管理器,它可以帮你发现和查看包 ...
随机推荐
- vs2010项目使用vs2013编译报错 无法打开包括文件:“winapifamily.h”
我的老项目是vs2010下的项目.最近安装vs2013后,打开sln解决方案,调试运行报错 C:\Program Files (x86)\Windows Kits\8.0\Include\um\win ...
- python爬虫感想
老师说,能用程序解决一个实际问题,说明已经会使用工具了.今天用python做了一个小爬虫,很幸运,成功了. 有几个难点:就是学会伪装,还有一个觉得打开的方式太多,有点糊涂,正则表达式也要加强了.
- Memcached,你懂的
一.Memcached简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网 ...
- sprint3个人总结
转眼间,这个学期又过去了,真的是忙碌的一个学期,却是有点充实,特别是我们的软件工程这门课程,我们这次做的是公众号开发功能,说实话,当初对这个项目有兴趣是因为自己也在弄自己一个300多人的公众号,对于这 ...
- OpenCV 轮廓基本特征
http://blog.csdn.net/tiemaxiaosu/article/details/51360499 OpenCV 轮廓基本特征 2016-05-10 10:26 556人阅读 评论( ...
- 【皇甫】☀ TreeSet
-Set: 元素是无序(存入和取出的顺序不一致),元素不可以重复 |-- HashSet: 底层数据结构是哈希表 HashSet是如何保证元素的唯一性的呢? 是通过元素的两个方法,hashCode和e ...
- Android仿“守望先锋”加载动画
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 效果图 实现思路 画一个小六边形 按效果图位置画七个小六边形 实现一个小六边形的显示与隐藏 ...
- CryptographicException异常处理方法
在调用System.Security.Cryptography.ProtectedData.Protect方法来保护私密信息时,IIS可能会报以下错误:CryptographicException: ...
- Java Garbage Collection基础详解------Java 垃圾回收机制技术详解
最近还是在找工作,在面试某移动互联网公司之前认为自己对Java的GC机制已经相当了解,其他面试官问的时候也不存在问题,直到那天该公司一个做搜索的面试官问了我GC的问题,具体就是:老年代使用的是哪中垃圾 ...
- NSURLSessionTask使用dispatch_semaphore 完成同步机制
在NSURLSessionTask发起网路请求时,一般是异步操作,如果需要进行同步等待的话,可采用dispatch_semaphore_t信号量基于计数器的一种多线程同步机制.但是在多个线程访问共有资 ...