$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. 读《编写可维护的JavaScript》第四章总结

    第四章 变量 函数和运算符 4.1 ① 变量声明 变量声明是通过var语句来完成的,并且所有的var语句都提前到包含这段逻辑的函数的顶部执行. function doSomething() { + v ...

  2. mysql 数据库 表字段添加表情兼容

    项目中的几个需要支持Emoji表情符号,手机自带的表情,其实添加也很简单: 1 修改数据库 配置my.cnf  init-connect='SET NAMES utf8mb4'             ...

  3. C# WebService服务Post提交

    public string WebServerTest(string PostData) { PostData = "jsonData=" + PostData; string P ...

  4. php : 基础(1)

    php基本语法形式 1.php的标记符 有以下形式: 形式1(推荐): <?php 这里要写符合php语法的语句 ?> 形式2: <script language="php ...

  5. slideDoor(学习某编程网站的,仅作记录和学习)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. POI 解析xls

    1.所需jar包 poi-3.6.jar poi-ooxml-3.6.jar 2.M.java package junit; import java.io.FileInputStream; impor ...

  7. SQL基础--序列

    序列是一种数据库对象,用来自动产生一组唯一的序号:序列是一种共享式的对象,多个用户可以共同使用序列中的序号. 序列的创建语法 CREATE SEQUENCE sequencename [INCREME ...

  8. IntelliJ IDEA中Maven项目的默认JDK版本

    在IntelliJ IDEA 15中使用Maven时,IDEA将默认的编译版本.源码版本设置为jdk5.编译项目的时候出现警告:"Warning:Java: 源值1.5已过时, 将在未来所有 ...

  9. 如何在页面进入时就加载js

    页面一打开就执行JS的代码! onLoadwindow.onLoad=function(){}window.onload=function(){ } 补充:window.onload和doucumen ...

  10. ActiveReports 11 新特性速递

    又到了一年一度,翘首期盼的ActiveReports11 即将发布,ActiveReports 10 表控件横空出世,成为中国式复杂报表的救星后,ActiveReports11 又会有哪些令人惊奇的新 ...