mongodb aggregate示例

阅读更多

 在

db.award.aggregate(

	// Pipeline
	[
		// Stage 1
		{
			$lookup: { "from" : "merchant" , "localField" : "merchantId" , "foreignField" : "_id" , "as" : "merchant"}
		},

		// Stage 2
		{
			$unwind: "$merchant"
		},

		// Stage 3
		{
			$match: {
			"merchant.name":{$regex:".*麦当劳.*"}
			}
		},

		// Stage 4
		{
			$project: {
			   "merchantId":1,"merchant.name":1,
			   "collectWordColumn":{
			     $cond:{
			         if:{
			             $eq:["$type","1"]
			         },then:1,else:0
			     }
			   },
			   "scratchColumn":{
			     $cond:{
			         if:{
			             $in:["$type",["2","3"]]
			         },then:1,else:0
			     }
			   },
			   "jigsawColumn":{
			     $cond:{
			         if:{
			             $eq:["$type","5"]
			         },then:1,else:0
			     }
			   },
			}
		},

		// Stage 5
		{
			$group: {
			"_id":"$merchantId",
			"collectWordCount" : { "$sum" : "$collectWordColumn"},
			"scratchCount" : { "$sum" : "$scratchColumn"},
			"jigsawCount" : { "$sum" : "$jigsawColumn"},
			"awardCount" : { "$sum" : 1},
			}
		},

		// Stage 6
		{
			$limit: 20
		},

		// Stage 7
		{
			$skip: 0
		},

		// Stage 8
		{
			$sort: {
			"_id":-1
			}
		},
	],

	// Options
	{
		cursor: {
			batchSize: 50
		}
	}

	// Created with Studio 3T, the IDE for MongoDB - https://studio3t.com/

);

 

你可能感兴趣的:(mongodb aggregate示例)