Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 知乎專欄 | 視頻教程 | About

14.11. aggregate

14.11.1. project

14.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", "."] } } }
]);			
				
				
14.11.1.2. substr
					db.getCollection('pyramidSelling').aggregate(
					[
					{
					$project: {
					userName: 1,
					phone: {
					prefix: { $substr: [ "$phone", 0, 3 ] },
					mobile: { $substr: [ "$phone", 3, 11 ] }
					},
					}
					}
					]
					)
				

14.11.2. groupby + sum

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

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