|
| 1 | +// Aggregate on data in MongoDB database |
| 2 | +// Uses Mongo shell commands (not a standalone script) |
| 3 | + |
| 4 | +use geography |
| 5 | + |
| 6 | + |
| 7 | +/* Simple queries */ |
| 8 | + |
| 9 | +print("--------------------------") |
| 10 | +print(".find(Amsterdam):") |
| 11 | +db.cities.find({name: "Amsterdam"}) |
| 12 | + |
| 13 | +amsterdam = db.cities.find({name: /^Amst[aoeu]rdam$/, country: "NL"})[0] |
| 14 | +print(".find(above amsterdam):") |
| 15 | +db.cities.find({'location.longitude': {$gt: amsterdam.location.longitude}}).count() |
| 16 | + |
| 17 | +var range = {} |
| 18 | +range['$lt'] = 1e6 |
| 19 | +range['$gt'] = 1e5 |
| 20 | +print(".find(mid-size cities ^P):") |
| 21 | +db.cities.find({name: /^P/, population: range}, {name: 1, _id: 0}) |
| 22 | + |
| 23 | +print("--------------------------") |
| 24 | +print("Nested queries:") |
| 25 | +db.cities.find( |
| 26 | + { |
| 27 | + $or: [ |
| 28 | + {famous_for: /light/, 'mayor.party': {$exists: true}}, |
| 29 | + {name: /^Londonder/} |
| 30 | + ], |
| 31 | + population: {$type: 'number'} |
| 32 | + }, |
| 33 | + {name: 1, famous_for: 1, mayor: 1, population: 1, _id: 0} |
| 34 | +) |
| 35 | + |
| 36 | +print("--------------------------") |
| 37 | +print("Updating") |
| 38 | +db.cities.update( |
| 39 | + {_id: amsterdam['_id']}, |
| 40 | + { |
| 41 | + $set: {'age': 550}, |
| 42 | + $inc: {'population': 1000}, |
| 43 | + $push: {'famous_for': "Anne Frankhuis"} |
| 44 | + } |
| 45 | +) |
| 46 | +db.cities.findOne({_id: amsterdam._id}) |
| 47 | + |
| 48 | +print("--------------------------") |
| 49 | +print("Adding indices for speed improvements") |
| 50 | + |
| 51 | +db.cities.createIndex({location: "2d"}) |
| 52 | +db.cities.find({location: {$near: [45.52, -122.67]}}).limit(5) |
| 53 | + |
| 54 | +db.phones.dropIndexes() |
| 55 | +// explain the plan, and include the results (executionStats) |
| 56 | +explained_before = db.phones.find({display: "+1 867-5309000"}).explain("executionStats") |
| 57 | +print(explained_before) |
| 58 | +db.phones.createIndex( |
| 59 | + {display: 1}, |
| 60 | + {unique: true, dropDups: true} |
| 61 | +) |
| 62 | +db.phones.createIndex( |
| 63 | + {'components.area': 1, 'components.number': 1}, |
| 64 | + {background: 1} // async |
| 65 | +) |
| 66 | +print(db.phones.getIndices()) |
| 67 | +explained_after = db.phones.find({display: "+1 867-5309000"}).explain("executionStats") |
| 68 | +print("Before: " + explained_before.executionStats.executionTimeMillis + "ms") |
| 69 | +print("After: " + explained_after.executionStats.executionTimeMillis + "ms") |
| 70 | + |
| 71 | + |
| 72 | +/* MapReduce & Aggregation */ |
| 73 | + |
| 74 | +// Simple aggregation built-ins |
| 75 | +print("--------------------------") |
| 76 | +print("Numbers before 5550005") |
| 77 | +db.phones.distinct( |
| 78 | + 'components.number', // field to return in array |
| 79 | + {'components.number': {$lt: 5550005}} // find |
| 80 | +) |
| 81 | +print("number of phones above 5599999 (slow, iterating on all)") |
| 82 | +const start_grp = new Date() |
| 83 | +db.phones.group({ // NOTE: officially doesn't work for sharded setups |
| 84 | + initial: {count: 0, max: 0}, // initial output |
| 85 | + reduce: function(phone, output) { |
| 86 | + // function to run on each item (item, ongoing_result) |
| 87 | + output.count++; |
| 88 | + output.max = Math.max(output.max, phone.components.number); |
| 89 | + }, |
| 90 | + cond: {'components.number': {$gt: 5599999}}, // find |
| 91 | + key: {'components.area': true} // group by |
| 92 | +}) |
| 93 | +print("Query (group built-in) took " + (new Date() - start_grp) + "ms") |
| 94 | + |
| 95 | +// Aggregation framework |
| 96 | +// Same query as before, but much quicker (20x on this machine) |
| 97 | +const start_aggr = new Date() |
| 98 | +db.phones.aggregate([ |
| 99 | + {$match: {'components.number': {$gt: 5599999}}}, |
| 100 | + {$group: { |
| 101 | + _id: '$components.area', |
| 102 | + count: {$sum: 1}, |
| 103 | + max: {$max: '$components.number'} |
| 104 | + }} |
| 105 | +]) |
| 106 | +print("Query (aggregation framework) took " + (new Date() - start_aggr) + "ms") |
| 107 | + |
| 108 | +// MapReduce framework |
| 109 | +const start_mr = new Date() |
| 110 | +load('map.js') // could also be saved server-side |
| 111 | +load('reduce.js') |
| 112 | +db.phones.mapReduce( |
| 113 | + map_count, |
| 114 | + reduce_count, |
| 115 | + { |
| 116 | + query: {'components.number': {$gt: 5599999}}, |
| 117 | + out: {inline: 1} |
| 118 | + } |
| 119 | +).results |
| 120 | +print("Query (MapReduce framework) took " + (new Date() - start_mr) + "ms") |
0 commit comments