[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 --> ...
随机推荐
- 初学JavaScript(入门一)
javaScript是世界上最流行的脚本语言 在我们的手机.电脑设备上所浏览的所有网页,以及基于HTML5手机App的交互都是通过javaScript驱动的,所以javascript是前端工作的一 ...
- mysql执行update报错1175解决方法
mysql执行update报错 update library set status=true where 1=1 Error Code: 1175. You are using safe update ...
- 【开源项目之路】jquery的build问题
在刚开始clone了jquery到本地build的时候,就遇到了问题. “ENORESTARGET No tag found that was able to satisfy ...” 提示为bowe ...
- c#中@符号作用
用 @ 符号加在字符串前面表示其中的转义字符“不”被处理. 如果我们写一个文件的路径,例如"D:/文本文件"路径下的text.txt文件,不加@符号的话写法如下: string f ...
- Problem About Salesforce SOAP API 32.0 In .Net Project
最近在集成项目项目中遇到一个问题:在用最新版本(API 32.0)Enterprise WSDL在.Net 中做集成时,初始化SforceService 时会初始化类错误.这算是Salesforce ...
- Chapter 3 Start Caffe with MNIST Demo
先从一个具体的例子来开始Caffe,以MNIST手写数据为例. 1.下载数据 下载mnist到caffe-master\data\mnist文件夹. THE MNIST DATABASE:Yann L ...
- 《学习OpenCV》练习题第四章第一题b&c
#include <highgui.h> #include <cv.h> #pragma comment (lib,"opencv_calib3d231d.lib&q ...
- HW7.14
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- html5 canvas 移动小方块
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- python知识点 07-11
python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量 python的 nonlocal关键字用来在函数或其他作用域中使用 ...