$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. Xcode8.0 去除控制台多余打印

    选择Product->Scheme->Edit Scheme ...或者直接按 command + shift + < 快捷键,在弹出的窗口中Environment Variable ...

  2. d3.js读书笔记-2

    比例尺 比例尺基本内容 比例尺是一组把输入域映射为输出范围的函数.任意数据集中的值不可能恰好与图表中的像素尺度一一对应.比例尺就是把这些数据值映射为可视化图形中使用的新值的便捷手段.D3的比例尺就是那 ...

  3. Using GET_APPLICATION_PROPERTY in Oracle D2k Forms

    Using GET_APPLICATION_PROPERTY in Oracle D2k Forms DescriptionReturns information about the current ...

  4. Webpack参考资料

    学习是一种进步,只有不断的向别人学习,才能提升自己.三人行必有我师焉 1. http://www.cnblogs.com/zhengjialux/p/5861845.html

  5. Entity Framework 数据库初始化四种策略

    策略一:数据库不存在时重新创建数据库 Database.SetInitializer<testContext>(new CreateDatabaseIfNotExists<testC ...

  6. 点餐系统Sprint1总结

    经过第一个阶段的冲刺,大家基本上都熟悉自己要做什么,并且把界面做了出来.通过这个阶段的冲刺,大家都学到了很多东西,团队更加默契,遇到不懂的都会提出来一起解决. 团队贡献分 148彭宏亮 20 150张 ...

  7. 我的git学习

    当遇到不想commit的,而status已经现实出来了,可以使用 git rm -r --cached "fine name or 文件夹" 出现   Git – fatal: U ...

  8. 解决 java 使用ssl过程中出现"PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"

    今天,封装HttpClient使用ssl时报一下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExc ...

  9. Sublime text3 常用插件 安装

    1 安装插件前的准备工作 首先确保你的Sublime Text3编辑器为官方版(非破解版),建议下载官网的便携版本(好处多多). 然后安装插件管理工具(Package Control) 1.1 打开S ...

  10. Android开源测试框架学习

    近期因工作需要,分析了一些Android的测试框架,在这也分享下整理完的资料. Android测试大致分三大块: 代码层测试 用户操作模拟,功能测试 安装部署及稳定性测试 代码层测试 对于一般java ...