db.createCollection("Animals") db.createCollection("People") db.Animals.insertMany([{name: "Fluffy", species: "sheep", weight: 40, owner:"Joe MacDonald"}, {name: "Honor", species:"horse", weight:200, color: "brown", owner: "Jaime Lannister", gender:"M"}, {name: "Rex", species:"Dog", weight:20, owner:"Joe MacDonald", color:"black"}, {species:"Horse", owner:"Joe MacDonald"},{name:"Vermithor", species:"dragon", owner:"Hugh Hammer", color:"bronze", size: "large"}, {name: "Stinky", species:"Pig", owner:"Joe MacDonald"},{name:"Cannibal", species:"dragon"}]) db.People.insertMany([{name:"Joe MacDonald", occupation: "farmer", weight: 76, height: 173, age: 83}, {name: "Hugh Hammer", occupation: "betrayer", height: 203}, {name:"Harry Blackstone Copperfield Dresden", occupation:"wizard"}]) db.Animals.aggregate([{$lookup: { from: "People", localField: "owner", foreignField: "name", as: "height", pipeline: [ {$project: { "height": 1, "_id": 0} }, ] } }, {$group: {_id: "$species", maxHeight: { $max: '$height' }}}, ]) db.Animals.aggregate([{$lookup: { from: "People", localField: "owner", foreignField: "name", as: "height", pipeline: [ {$project: { "height": 1, "_id": 0} }, ] } }, {$group: {_id: "", maxHeight: { $max: '$height' }}}, ]) db.Animals.aggregate( [ { $group:{ _id:"$owner", animals: {$count: {}}} } ] ) db.Animals.aggregate( [ { $group:{ _id:["$owner","$species"], animals: {$count: {}}} } ] ) for (var i = 1; i <= 25; i++) { db.Numbers.insertOne({ x : i , type:"Integer"}) } db.People.aggregate( [ { $addFields: { newHeight: { $function: { body: function(height) { if (height === parseInt(height, 10)) return height; else if (typeof height === string || height instanceof String) return +height.substring(0,height.length-2)*100; else return -1; }, args: [ "$height" ], lang: "js" } }, } } ] )