mongogogog
$cmp,$eq,$gt,$gte,$lt,$lte,$ne
$setEquals,$setIntersection,$setUnion,$setDifference,$setLsSubset,$anyElementTrue,$allElemetsTrue
x: { $cond: { if: { $gte: [ "$qty", 250 ] }, then: 30, else: 20 } }
x: {$cond : [{ $ne : ['$x', NaN] }, '$x', 0]}
x: {$cond: [ { $gte: [ "$qty", 250 ] }, 30, 20 ]}
{ $inc: { quantity: -2, "metrics.orders": 1 } }
{ $mul: { price: 1.25 } } ==price*1.25
"name": { "$concat": [ "$firstName", " ", "$lastName" ] }
totalAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } },
avgQuantity: { $avg: "$quantity" }
$first,$last,$max,$min,$push,$addToSet,
1,日期转换
db.sales.aggregate(
[
{
$project:
{
year: { $year: "$date" },
month: { $month: "$date" },
day: { $dayOfMonth: "$date" },
hour: { $hour: "$date" },
minutes: { $minute: "$date" },
seconds: { $second: "$date" },
milliseconds: { $millisecond: "$date" },
dayOfYear: { $dayOfYear: "$date" },
dayOfWeek: { $dayOfWeek: "$date" },
week: { $week: "$date" },
yearMonthDay: { $dateToString: { format: "%Y-%m-%d", date: "$date" } },
}
}
]
)
2,静态变量
db.records.aggregate( [
{ $project: { costsOneDollar: { $eq: [ "$price", 100 ] } } }
] )
{ $literal: "$1" }==“$1" { $literal: { $add: [ 2, 3 ] } == { "$add" : [ 2, 3 ] }
db.records.aggregate( [
{ $project: { costsOneDollar: { $eq: [ "$price", { $literal: "$1" } ] } } }
] )
3,更新options
var options={"upsert":false,"multi":false,'new':false}; 不存是否插入,更新是否批量,返回内容是更新前还是后.
4,{ $unwind: { path: "$sizes", preserveNullAndEmptyArrays: true } } || { $unwind: { path: "$sizes", includeArrayIndex: "arrayIndex" }
5,new mongoose.Types.ObjectId(id)
6,delete array
db.products.update({ },{ $pull: { results: { item: "A" } } })
> So to unset: update({$unset: { 'messages.2': 1 }}
> Then to pull null: update({$pull: { 'messages': null }})
7,db.products.update({ _id: 1 },{ $set: { item: "apple" },$setOnInsert: { defaultQty: 100 }} { upsert: true }) } } })
8,数组更新
var update={ $addToSet: { storeList:info}};数组加
update={$set: {"storeList.$":info}};数组修改
{$push:{"relationships":json}
db.test3.update( { _id: 6 }, { $pop: { grades: -1 } }); //从头删除
db.test3.update( { _id: 6 }, { $pop: { grades: 1 } }); //从尾删除
//把满足score大于90的grades,数组的第一个元素设置成88
db.students.update( { score: {$gt:90} },
{ $set: { "grades.$" : 88 } } ,
{ multi:true }
);
db.test2.insert(
{ "content" : "this is a blog post.", "comments" :
[ { "author" : "Mike", "comment" : "I think that blah blah blah...", },
{ "author" : "John", "comment" : "I disagree." }
]
}
);
/查找名为Mike的记录,并且该人的名字改成tank
db.test2.update( { "comments.author": "tank"},
{ $set: { "comments.$.author" :"xxxxx" } } )
db.test3.update(
{ grades: "aaa" },
{ $pull: { grades: "aaa" } }, //支持这种查找或匹配 $pull: { votes: { $gte: 6 } }
{ multi: true }
查看复制打印?
db.students.update( { _id: {$gt:1} },
{ $pullAll: { "grades": [90,92] } } //只支持数组
);
$push 向数组中添加元素
$each 循环数据
$sort 对数组进行排序
$slice 对整个collection表进行数据裁减,用的时候一定要当心
$position 插入数据的位置。
db.test4.insert(
{
"_id" : 5,
"quizzes" : [
{ wk: 1, "score" : 10 },
{ wk: 2, "score" : 8 },
{ wk: 3, "score" : 5 },
{ wk: 4, "score" : 6 }
]
}
);
db.test4.update( { _id: 5 },
{ $push: { quizzes: { $each: [ { wk: 5, score: 8 },
{ wk: 6, score: 7 },
{ wk: 7, score: 6 } ],
$sort: { score: -1 },
$slice: 3,
$position:2
}
}
}
);
mongogogog的更多相关文章
随机推荐
- 1. ReactNative 基础
/** 1. reactNative 反应式语言 2. 既拥有原生的用户体验,又保留React的开发效率 3. FaceBook研究 H5,Android,iOS 4. BAT的插件化,热修改 2 ...
- Xcode6 ADD Copy Files Build Phase 是灰色的
在学习的怎样写frameWork的时候,查看一个教程How to Create a Framework for iOS [一个中文翻译 创建自己的framework] 其中一个步骤就是添加一个Cop ...
- linux搭建LAMP
先简要概述下本文要点:(操作系统采用CentOS6.5 x64) 1.分别安装搭建lamp服务环境: 2.采用lamp一键安装包搭建环境: 3.在lamp环境中初步搭建起一个网站: 一. 分别安装搭建 ...
- consul 模板配置内容的参数说明
datacenters {{datacenters}} 数据中心 file {{file "/path/to/local/file"}} 读取本地文件的内容.如果不可读的话,会报错 ...
- TFS二次开发系列:六、TFS的版本控制
在TFS中对于版本控制是在WorkSpace工作区来控制的. 首先我们先整理WorkSpace的一些基本使用方法. CheckIn:迁入挂起的操作 CreateMapping:创建一个本地映射地址 D ...
- 嵌入式Linux学习笔记(0)基础命令。——Arvin
学习记录: 到今天为止ARM裸机开发学习进程:1.2.1-1.2.14 预科班知识Linux介绍学习进程:0.2.1-0.2.6 学习内容笔记: 学习了Linux的开发方式的优劣介绍 学习了常用文件夹 ...
- 20161117__修改yum源
1.CentOS6.5中修改yum源 http://www.cnblogs.com/liuling/p/2014-4-14-001.html 在自己安装的CentOS6.5中使用yum安装软件,总是提 ...
- UIStackView使用 (堆视图)
一基本使用 1创建多个子控件 for (int i = 0; i < 3; i++) { UIButton *imgBtn = [UIButton buttonWithType:UIButton ...
- npm install报错Error: ENOENT
E:\projects\ueditor\ueditor1_4_3_3-src>npm installError: ENOENT, stat 'C:\Users\Lucas\AppData\Roa ...
- 在Spring里进行单元测试Junit
搭建Spring环境(自行搭建): @RunWith注解指定使用springJunit的测试运行器 @ContextConfiguration注解指定测试用的spring配置文件的位置 import ...