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的更多相关文章
随机推荐
- 理解 JavaScript 作用域和作用域链
http://www.cnblogs.com/lhb25/archive/2011/09/06/javascript-scope-chain.html
- OC中字符串的提取与替换-四种不同方法实现
/* 1.将可变字符串 @"When I was young, I loved a girl in neighbor class."中,从 young提取到girl.替换 成@&q ...
- iStylePDF安全电子文档解决方案之电子合同在线订立
交易是商业世界不可或缺的一部分,而签名是交易的凭证.可是,尽管互联网和IT技术已经很发达,但每逢遇到签名,还是得用最原始的方法——握笔写字.与如今走到哪都能听到“互联网+”相比有点不合潮流,通过电子签 ...
- Control.DataBinding数据绑定简单用法:
DataBindings的用法: 第一个值:要绑定到TextBox的什么地方 第二个值:数据源是什么 第三个值:应该取数据源的什么属性 第四个值:是否开启数据格式化 第五个值:在什么时候启用数据源绑定 ...
- 【积累】validate验证框架的使用
validate验证框架的使用:用验证框架可以很方便的验证前端页面输入的内容可以自定义验证方法 内容:0:环境搭建 1:基础用法 2:自定义用法 0:基本环境的搭建 0.1:下载js文件 0.2:引入 ...
- Gevent中信号量的使用
greenlet间同步方法:信号量 1.为什么引入信号量: 2.gevent信号量有哪些: 3.编程实现. 为何引入信号量 信号量是一个允许Greenlet相互合作,限制并发访问或运行的低层次的同步原 ...
- unity自带寻路Navmesh入门教程(一)
说明:从今天开始,我阿赵打算写一些简单的教程,方便自己日后回顾,或者方便刚入门的朋友学习.水平有限请勿见怪.不过请尊重码字截图录屏的劳动,如需转载请先告诉我.谢谢! unity自从3.5版本之后,增加 ...
- phantomjs+selenium实现爬取动态网址
之前使用 selenium + firefox驱动浏览器来实现爬取动态网址,但是firefox经常更新,更新后时常会导致webdriver启动不来,所以改用phantomjs+selenium来改善一 ...
- [CentOS 7] 安装nginx
下载对应当前系统版本的nginx包(package) # wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-cent ...
- mysql执行大量sql语句
今天需要通过csv上传大量数据到数据库 直接逐行执行insert效率极其低下 后面通过拼接insert,稍微提高了些许效率,但依然不满足 最后发现,把这些插入放入同一个事务里面可以大大提高效率 beg ...