$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
}
}
}
);

 posted on 2016-11-12 01:00  jayruan  阅读(203)  评论(0编辑  收藏  举报