Last active
December 3, 2022 18:01
-
-
Save sujeet-agrahari/0d04daebb681344483257755e590cb25 to your computer and use it in GitHub Desktop.
mongodb-aggregation
This file contains hidden or 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
// MongoDB Playground | |
// Use Ctrl+Space inside a snippet or a string literal to trigger completions. | |
const database = 'ecommerce'; | |
// The current database to use. | |
use(database); | |
// Create a new collection. | |
// db.createCollection(collection); | |
// The prototype form to create a collection: | |
/* db.createCollection( <name>, | |
{ | |
capped: <boolean>, | |
autoIndexId: <boolean>, | |
size: <number>, | |
max: <number>, | |
storageEngine: <document>, | |
validator: <document>, | |
validationLevel: <string>, | |
validationAction: <string>, | |
indexOptionDefaults: <document>, | |
viewOn: <string>, | |
pipeline: <pipeline>, | |
collation: <document>, | |
writeConcern: <document>, | |
timeseries: { // Added in MongoDB 5.0 | |
timeField: <string>, // required for time series collections | |
metaField: <string>, | |
granularity: <string> | |
}, | |
expireAfterSeconds: <number>, | |
clusteredIndex: <document>, // Added in MongoDB 5.3 | |
} | |
)*/ | |
// More information on the `createCollection` command can be found at: | |
// https://www.mongodb.com/docs/manual/reference/method/db.createCollection/ | |
// db.persons.aggregate([ | |
// {/** | |
// * query: The query in MQL. | |
// */ | |
// $match: { | |
// "gender": "male" | |
// }}, | |
// { | |
// /** | |
// * Provide the number of documents to limit. | |
// */ | |
// $limit: 2 | |
// } | |
// ]) | |
// db.persons.aggregate([ | |
// { | |
// /** | |
// * query: The query in MQL. | |
// */ | |
// $match: { | |
// $or: [{gender: "male"}, { gender: "female"}] | |
// } | |
// }, | |
// { | |
// /** | |
// * _id: The id of the group. | |
// * fieldN: The first field name. | |
// */ | |
// $group: { | |
// _id: "$gender" | |
// } | |
// } | |
// ]).sort({ _id: 1}) | |
// db.persons.aggregate([ | |
// { /** | |
// * query: The query in MQL. | |
// */ | |
// $match: { | |
// gender: 'female' | |
// }}, | |
// { | |
// /** | |
// * _id: The id of the group. | |
// * fieldN: The first field name. | |
// */ | |
// $group: { | |
// _id: { | |
// state: "$location.state" | |
// }, | |
// totalPerson: { | |
// $sum: 1 | |
// } | |
// } | |
// }, | |
// { | |
// /** | |
// * Provide any number of field/order pairs. | |
// */ | |
// $sort: { | |
// totalPerson: -1 | |
// } | |
// } | |
// ]) | |
// db.persons.aggregate([ | |
// { | |
// /** | |
// * specifications: The fields to | |
// * include or exclude. | |
// */ | |
// $project: { | |
// _id: 0, | |
// name: 1, | |
// email: 1, | |
// location: { | |
// type: "Point", | |
// coordinates: [ | |
// {$convert: {input: "$location.coordinates.longitude", to: "double", onError: 0, onNull: 0}}, | |
// {$convert: {input: "$location.coordinates.latitude", to: "double", onError: 0, onNull: 0}}, | |
// ] | |
// }, | |
// age: "$dob.age", | |
// birthdate: { | |
// $toDate: "$dob.date" | |
// } | |
// } | |
// }, | |
// { | |
// /** | |
// * specifications: The fields to | |
// * include or exclude. | |
// */ | |
// $project: { | |
// gender: 1, | |
// location: 1, | |
// email:1, | |
// age: 1, | |
// birthdate: 1, | |
// fullName: { | |
// $concat: [ | |
// { $toUpper: { $substrCP: ['$name.first', 0, 1] } }, | |
// { | |
// $substrCP: [ | |
// '$name.first', | |
// 1, | |
// { $subtract: [{ $strLenCP: '$name.first' }, 1] }, | |
// ], | |
// }, | |
// ' ', | |
// '$name.last', | |
// ], | |
// }, | |
// }, | |
// }, | |
// { | |
// /** | |
// * _id: The id of the group. | |
// * fieldN: The first field name. | |
// */ | |
// $group: { | |
// _id: { | |
// birthYear: { | |
// $isoWeekYear: "$birthdate" | |
// } | |
// }, | |
// totalPerson: { | |
// $sum: 1 | |
// } | |
// } | |
// }, | |
// { | |
// /** | |
// * Provide any number of field/order pairs. | |
// */ | |
// $sort: { | |
// totalPerson: -1 | |
// } | |
// } | |
// ]); | |
// db.exams.find() | |
// db.persons.find() | |
// db.exams.aggregate([ | |
// { | |
// /** | |
// * path: Path to the array field. | |
// * includeArrayIndex: Optional name for index. | |
// * preserveNullAndEmptyArrays: Optional | |
// * toggle to unwind null and empty values. | |
// */ | |
// $unwind: "$hobbies" | |
// }, | |
// { | |
// /** | |
// * _id: The id of the group. | |
// * fieldN: The first field name. | |
// */ | |
// $group: { | |
// _id: { | |
// age: "$age", | |
// }, | |
// allHobbies: { | |
// $addToSet: "$hobbies" | |
// } | |
// } | |
// } | |
// ]) | |
// db.exams.aggregate([ | |
// { | |
// /** | |
// * specifications: The fields to | |
// * include or exclude. | |
// */ | |
// $project: { | |
// _id: 0, | |
// examScore: { | |
// $slice: ["$examScores", 2, 1] | |
// }, | |
// totalScore: { | |
// $size: "$examScores" | |
// } | |
// } | |
// } | |
// ]) | |
db.exams.aggregate([ | |
{ | |
/** | |
* specifications: The fields to | |
* include or exclude. | |
*/ | |
$project: { | |
_id: 0, | |
examScore: { | |
$filter: { | |
input: '$examScores', | |
as: 'sc', | |
cond: { | |
$gt: ['$$sc.score', 60], | |
}, | |
}, | |
}, | |
}, | |
}, | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment