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" }
    } 
}])