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.pairs.find( { _id.a: "u123", count: { $gt: 10 }} ) | |
.sort({ count: -1 }) |
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
{ _id: { a: 2, b: 3 }, value: 36 } |
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
var map = function() { | |
for (i = 0; i < this.items.length; i++) { | |
for (j = i + 1; j <= this.items.length; j++) { | |
emit({a: this.items[i] ,b: this.items[j] }, 1); // emit each item pair | |
} | |
} | |
} | |
var reduce = function(key, values) | |
{ return Array.sum(values); } // Sum all occurrences of each item pair |
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
{ _id: "u123", items: [ 2, 3, 8 ] } |
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
var map = function() { emit(userId, this.itemId); } // emit samples as userId: itemId | |
var reduce = function(key, values) | |
{ return values; } // Return the array of itemIds the user has bought | |
db.activities.mapReduce(map, reduce, | |
{ query: { type: “ORDER”, time: { $gt: NOW-24H }}, // include only activities of type order int he last day | |
out: { replace: "lastDayOrders", sharded: true }) // persist the output to a collection |
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.lastHourUniques.count() |
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.lastHourUniques.find({ userId: "u123" }) |
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
var map = function() { emit(this.userId, 1); } // emit samples by userId with value of 1 | |
var reduce = function(key, values) | |
{ return Array.sum(values); } // sum the total samples per user | |
db.activities.mapreduce(map, reduce, | |
{ query: { time: { $gt: NOW-1H } }, // time bound the query to the last hour | |
out: { replace: "lastHourUniques", sharded: true }) // persist the output to a collection |
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.activities.aggregate(([ | |
{ $match: { time: { $gt: NOW-1H } }}, | |
{ $group: { _id: "$userId", count: {$sum: 1} } }], | |
{ allowDiskUse: 1 }) |
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.activities.aggregate(([ | |
{ $match: { itemId: "301671", ts: { $gt: DATE }}}, | |
{ $group: { _id: "$type", count: { $sum: 1 }}}]) |