mongodb aggregate $match $group $sum $push $$ROOT
阿新 • • 發佈:2018-11-10
> db.task.aggregate([{$match: {"status": {$exists: false}}}, {$group: {_id: "$status", count: {$sum: 1}, result: {$push: "$$ROOT"}}}]).pretty()
> db.task.aggregate([{$match: {"status": {$exists: true}}}, {$group: {_id: "$status", count: {$sum: 1}, result: {$push: "$$ROOT"}}}]).pretty()
> db.task.aggregate([{$match: {"status": {$exists: false}}}, {$limit: 3}, {$group: {_id: "$status", count: {$sum: 1}, result: {$push: "$$ROOT"}}}]).pretty()
> db.task.aggregate([{$match: {"status": {$exists: true}}}, {$limit: 3}, {$group: {_id: "$status", count: {$sum: 1}, result: {$push: "$$ROOT"}}}]).pretty()
> db.task.aggregate([{$match: {"status": {$exists: true}}}, {$group: {_id: "$status", count: {$sum: 1}, result: {$push: "$$ROOT"}}}]).pretty()
> db.task.aggregate([{$match: {"status": {$exists: false}}}, {$limit: 3}, {$group: {_id: "$status", count: {$sum: 1}, result: {$push: "$$ROOT"}}}]).pretty()
> db.task.aggregate([{$match: {"status": {$exists: true}}}, {$limit: 3}, {$group: {_id: "$status", count: {$sum: 1}, result: {$push: "$$ROOT"}}}]).pretty()