Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | Github | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

44.11. aggregate

44.11.1. project

44.11.1.1. $split
				
{
	"_id" : ObjectId("591a710320156761bdf68a06"),
	"_class" : "mis.domain.PyramidSelling",
	
	...
	...
	
	"status" : true,
	"createdDate" : ISODate("2017-05-16T03:24:51.511Z")
}
					
				
				
db.getCollection('pyramidSelling').aggregate([
  { $project : { _class : { $split: ["$_class", "."] } } }
]);			
				
				
44.11.1.2. substr
				
db.getCollection('pyramidSelling').aggregate(
[
	{
	$project: {
		userName: 1,
			phone: {
				prefix: { $substr: [ "$phone", 0, 3 ] },
				mobile: { $substr: [ "$phone", 3, 11 ] }
			},
		}
	}
]
)
				
				

44.11.2. groupby + sum

select username, sum(balance) as total from users group by member.

			
db.member.aggregate([{ 
    $group: { 
        _id: "$username", 
        total: { $sum: "$balance" }
    } 
}])