Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | Github | OSChina 博客 | 云社区 | 云栖社区 | Facebook | Linkedin | 知乎专栏 | 视频教程 | About

45.11. aggregate

45.11.1. project

45.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", "."] } } }
]);			
				
				

45.11.1.2. substr

					db.getCollection('pyramidSelling').aggregate(
					[
					{
					$project: {
					userName: 1,
					phone: {
					prefix: { $substr: [ "$phone", 0, 3 ] },
					mobile: { $substr: [ "$phone", 3, 11 ] }
					},
					}
					}
					]
					)
				

45.11.2. groupby + sum

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

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