[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 --> ...
随机推荐
- 插入排序 --- 排序算法 --- 算法 --- java
设数组为a[0…n-1]. 1. 初始时,a[0]自成1个有序区,无序区为a[1..n-1].令i=1 2. 将a[i]并入当前的有序区a[0…i-1]中形成a[0…i]的有序区间 ...
- 《VC++ 6简明教程》即VC++ 6.0入门精讲 学习进度及笔记
VC++6.0入门→精讲 2013.06.09,目前,每一章的“自测题”和“小结”三个板块还没有看(备注:第一章的“实验”已经看完). 2013.06.16 第三章的“实验”.“自测题”.“小结”和“ ...
- JavaScript高级程序设计(第三版)第四章 变量,作用域和内存问题
JavaScript变量可以用来保存两种类型的值:基本类型值和引用类型值.基本类型值和引用类型值具有以下特点: 基本类型值在内存中占据固定大小的空间,因此被保存在栈内存中: 从一个变量向另一个变量复制 ...
- FlatBuffers与protobuf性能比较
FlatBuffers发布时,顺便也公布了它的性能数据,具体数据请见Benchmark. 它的测试用例由以下数据构成"a set of about 10 objects containing ...
- xargs 简单功能
之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了xargs命令,例如: find /sbin -perm +700 |ls -l 这个命 ...
- 转载-SQL中的where条件,在数据库中提取与应用浅析
1 问题描述 一条SQL,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当然,要完整描述一条SQL在数据库中的生命周期,这是一个非常巨大的问题,涵盖了SQL的词法解析.语 ...
- c++面试题总结(2)
1. C中static有什么作用 (1)隐藏. 当我们同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性,故使用static在不同的文件中定义同名函数和同名变量,而不必担心命 ...
- 【转】MySQL索引和查询优化
原文链接:http://www.cnblogs.com/mailingfeng/archive/2012/09/26/2704344.html 对于任何DBMS,索引都是进行优化的最主要的因素.对于少 ...
- gdb之watch命令
[gdb之watch命令] 什么是watchpoint? watchpoint,顾名思义,其一般用来观察某个变量/内存地址的状态(也可以是表达式),如可以监控该变量/内存值是否被程序读/写情况. 在g ...
- Linux查找文件夹名
@(编程) find / -type d -name filename type的类型 -type c File is of type c: b block (buffered) special c ...