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的更多相关文章
随机推荐
- Hive数据仓库
			Hive 是一个基于Hadoop分布式文件系统(HDFS)之上的数据仓库架构,同时依赖于MapReduce.适用于大数据集的批处理,而不适用于低延迟快速查询. Hive将用户的HiveQL语句转换为M ... 
- 把数据输出到Word (组件形式)
			上一篇的文章中我们介绍了在不使用第三方组件的方式,多种数据输出出到 word的方式,最后我们也提到了不使用组件的弊端,就是复杂的word我们要提前设置模板.编码不易控制.循环输出数据更是难以控制.接下 ... 
- winform异步系统升级—BackgroundWorker
			BackgroundWorker用法实例 自己的代码,就是要执行的代码写到dowork里,ProgressChanged事件是控制进度时用的,最后的Completed事件进度完成,也就是dowork里 ... 
- Oracle 新增删除账户
			新增用户: create user test identified by 123456;grant dba,connect,resource to test; 删除账户: drop user xxx ... 
- PHPStorm技巧篇 -- 观感优化
			(1)设置默认显示行号 (2)设置自动换行 (3)去除代码下划线(拼写检测) 优化说明:自动换行和显示行号字面意思很好理解,下划线说明一下,phpstorm默认对代码进行拼写校验,即对于不符合英文单词 ... 
- eclipse +maven+ssm搭建矿建
			记录一下搭建框架的过程1.下载最新的eclipse https://www.eclipse.org/downloads/download.php?file=/oomph/epp/neon/R/ec ... 
- android持久化技术
			Android系统提供了3种持久化技术,所谓持久化技术是指将内存中的书籍保存在存储设备中. 1.文件存储 2.sharedPreference存储 3.数据库存储 除此之外,还可以将数据保存在SD卡中 
- 《开源安全运维平台:OSSIM最佳实践》内容简介
			<开源安全运维平台:OSSIM最佳实践 > 李晨光 著 清华大学出版社出版 内 容 简 介在传统的异构网络环境中,运维人员往往利用各种复杂的监管工具来管理网络,由于缺乏一种集成安全运维平台 ... 
- reqwest请求api和约束(转载)
			转自:https://www.oschina.net/p/reqwest reqwest 用于浏览器异步HTTP请求.支持xmlHttpRequest, JSONP, CORS, 和 CommonJS ... 
- 解决VS2012编写JQuery代码不能智能提示的问题(其他js库的代码提示设置估计类似)
			VS默认设置下编写jQuery代码是这样的: 解决办法: 1.在项目的"管理NuGet程序包"中安装JQuery: 2.打开:工具 -> 选项 -> 文本编辑器 -&g ... 
