mongo聚合
{ $match: { avgTime: { $gt: 7 } } }
mongo $limmit use
{ $limit: 100 },
{ $limit: 10 }
取最小
mongo $skip use
{ $skip: 5 },
{ $skip: 2 }
取两个总和
mongo $match use
{ $match: { year: 2014 } },
{ $match: { status: "A" } }
{ $match: { $and: [ { "year" : 2014 }, { "status" : "A" } ] } }
相当于and
mongo $lookup and $unwind
{
$lookup: {
from: "otherCollection",
as: "resultingArray",
localField: "x",
foreignField: "y"
}
},
{ $unwind: "$resultingArray"}
$sort+ $skip+ $limit使用
{ $sort: { age : -1 } },
{ $skip: 10 },
{ $limit: 5 }
单用途聚合操作
db.distinct('cue_id')
return 1
{ $match: { name: "Joe Schmoe" } },
{ $addFields: {
maxTime: { $max: "$times" },
minTime: { $min: "$times" }
} },
{ $match: { maxTime: { $lt: 20 }, minTime: { $gt: 5 } } },
{ $project: {
_id: 1, name: 1, times: 1, maxTime: 1, minTime: 1,
avgTime: { $avg: ["$maxTime", "$minTime"] }
示例:
project = {'_id': 0, "title": 1, "link": "$pdf_link", "abstract": 1}
pipeline = [{"$match": {'$or':[{'company_name': company_name},{'code':company_name}]}}, {"$sort": {"updated_at": -1}},
{"$limit": 5}, {'$project': project}]
db.aggregate(str(pipeline), collection, BASE)
db.aggregate(str([{'$match:{'fileds':A}'},{'$group':{'_id':'$cue_id','totle':{'$sum':$amount}}}],collection, BASE))
mongo聚合
最新推荐文章于 2024-07-28 18:58:12 发布