[MongoDB] Query, update, index and group

/*
1. Query Operators
*/
db.posts.find({
viewsCount: {$get: 1000, $lte: 3000}
}, {_id: 0, viewsCount: 1, title: 1}) // $in
db.posts.find({
categories: {$in: ['ios']}
}, {categories: 1}) //$where
db.posts.find({
$where: function(){
return this.categories.length>15
}
}, {categories: 1, title: 1}) //see how many categories from previous query:
db.posts.find({
$where: function(){
return this.categories.length>15
}
}, {categories: 1, title: 1})[0].categories.length
Update:
/*
2. Update
*/
var d = db.posts.findOne();
//change the author email:
d.author.email = "new@new.com"
//update the database
db.post.update({_id: d._id}, d); //update mutli-document
db.posts.update({viewsCount: {$gt: 3000}}, {$set: {newKey: true}}, {multi:true}) //add new key, set mutli

Performance and indexes:
/*
3. Performance and indexes
*/
//create the data
db.numbers.drop();
var num = 5000000,
i = 0;
for(i = 0; i < num; i ++){
var randomNumber = Math.floor(Math.random()*10000);
db.numbers.insert({number:randomNumber});
//print(i);
} db.number.find({number: {$gt: 1000, $lt: 3000}, {_id: 0, number: 1}}).explain(); //it scan all the doucment, took 3500ms //add index:
db.numbers.ensureIndex({number: 1});
//took 1046 ms, scan 1m instead of 5m

Group:
/*
4. $group
*/ //sum up the viewCount, rename the result as 'totalViewsCount'
db.posts.aggregate({
$group: {
_id:null,
totalViewsCount: {$sum: '$viewsCount'}
}
}) //adding 1 for each document
db.posts.aggregate({
$group: {
_id:null,
totalNumberOfDocuments: {$sum: 1}
}
}) // =
db.posts.count()
[MongoDB] Query, update, index and group的更多相关文章
- java.lang.IllegalArgumentException: Illegal character in query at index 261
在BaseFragment中使用了LoadingPage,而LoadingPage的联网加载使用的是AsyncHttpClient.一直报java.lang.IllegalArgumentExcept ...
- [转]使用Maven添加依赖项时(Add Dependency)时,没有提示项目可用,并且在Console中,输出: Unable to update index for central|http://repo1.maven.org/maven2 。
使用Maven添加依赖项时(Add Dependency)时,没有提示项目可用,并且在Console中,输出: Unable to update index for central|http://re ...
- MongoDB的update有关问题(JAVA)——如何一次更新所有的相同记录
MongoDB的update问题(JAVA)——怎么一次更新所有的相同记录用如下这个函数:public WriteResult update(DBObject q, DBObject o, boo ...
- Unable to update index for central
Unable to update index for central http://repo1.maven.org/maven2/ 就是这句,myeclipse启动后控制台输出这句话:解决办法:1.在 ...
- 爬取数据时解析url时一直报错Caused by: java.net.URISyntaxException: Illegal character in query at index 823替换了所有空格和特殊字符还是无效
近日在用HttpClient访问抓取汇率时,为了省力,直接采用 String url = "http://api.liqwei.com/currency/?exchange=usd|cny& ...
- 解决eclipse安装maven的问题:Unable to update index for central|http://repo1.maven.org/maven2
问题产生如下:因为单位使用了过滤,访问Internet时,超过10M的内容就拒绝.因为maven插件在初始时,需要下载Maven的index文件,这个文件比较大,有38M多,下载不成功.所以造成使用M ...
- Unable to update index for central http://repo1.maven.org/maven2/ 解决方法
不知道什么原因 MyEclipse(eclipse) 中的 maven 插件突然不能用了,修改 pom.xml 无任何反应 控制台报 Unable to update index for centra ...
- java.lang.IllegalArgumentException: Illegal character in query at index ...解决办法
今天在写智能机器人问答实现的时候遇到了一个问题,就是我发送消息不能输入空格 给我报了一个错误java.lang.IllegalArgumentException: Illegal character ...
- 解决MyEclipse开启后总是不停的在Update index
近期MyEclipse开启之后总是不停的在 update index,非常是耗时间. 查找资料发现Update index...是Maven在不断更新, 解决的方法例如以下: Window --> ...
随机推荐
- C#面向对象基础01
面向对象不是取代面向过程的类.对象."人"是类,"张三"是人这个类的对象.类是抽象的,对象是具体的.按钮就是类,某个按钮就是对象.对象可以叫做类的实例.类就像i ...
- 两个实用的Python的装饰器
两个实用的Python的装饰器 超时函数 这个函数的作用在于可以给任意可能会hang住的函数添加超时功能,这个功能在编写外部API调用 .网络爬虫.数据库查询的时候特别有用 timeout装饰器的代码 ...
- xdebug初步
;加载xdebug模块. 根据PHP版本来选择是zend_extension还是zend_extension_ts ts代表线程安全 被坑过1次zend_extension="\web\ ...
- Javascript模板及其中的数据逻辑分离思想(MVC)
#Javascript模板及其中的数据逻辑分离思想 ##需求描述 项目数据库的题目表描述了70-120道题目,并且是会变化的,要根据数据库中的数据描述,比如,选择还是填空题,是不是重点题,题目总分是多 ...
- Jersey Rest服务类型
在Rest服务中,资源类是接收Rest请求并完成响应的核心类,而资源类由Rest服务的“提供者”来调度的,这一定义类似于自定义Servlet类,该类会奖请求分派给指定的Controller/Actio ...
- Hadoop2学习记录(1) |HA完全分布式集群搭建
准备 系统:CentOS 6或者RedHat 6(这里用的是64位操作) 软件:JDK 1.7.hadoop-2.3.0.native64位包(可以再csdn上下载,这里不提供了) 部署规划 192. ...
- bzoj3717: [PA2014]Pakowanie
Time Limit: 90 Sec Memory Limit: 256 MBSubmit: 128 Solved: 43[Submit][Status][Discuss]Description 你有 ...
- Strider SSH Deploy配置
登录需要ssh, ssh 免密码登录配置自行百度.shell里写成自己的需要的命令
- 第二百三十四天 how can I 坚持
今天果然不负众望,下了一天的雪啊,挺好. 今天把花搞了下,都弄花盆里了,希望不会就这么挂掉.八千代,绿萝,还有小叶元宝. 中午喝了点酒,没感觉. 过两天气温就零下十多度了,该咋办啊,最怕冬天.家里现在 ...
- NativeInt
NativeInt ni = 100; //Wrong. Will issue an Error NativeInt ni = NativeInt(100); //Correct System::Ge ...