-
-
Save 3axap4eHko/33aedfb8563a758d16993ea7ef2c9ba1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.createCollection('a'); | |
db.a.insertMany([ | |
{ day: 'Monday', packages: 1000 }, | |
{ day: 'Tuesday', packages: 100 }, | |
{ day: 'Monday', packages: 1000 }, | |
]); | |
db.a.createIndex({ day: 1 }); | |
db.createCollection('b', { viewOn: 'a', pipeline: [ | |
{ | |
$group: { | |
_id: { day: '$day' }, | |
packages: { $sum: '$packages' }, | |
} | |
} | |
] }); | |
db.b.find({ day: 'Monday' }).explain(); | |
/* | |
The result is | |
{ | |
"$group" : { | |
"_id" : { | |
"day" : "$day" | |
}, | |
"packages" : { | |
"$sum" : "$packages" | |
} | |
} | |
}, | |
{ | |
"$match" : { | |
"day" : { | |
"$eq" : "Monday" | |
} | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment