Skip to content

Instantly share code, notes, and snippets.

db.activities.aggregate(([
{ $match: { userId:"u123", ts:{$gt:DATE}, type:"ORDER"}},
{ $group: { _id: "result", count: {$sum: "$total" }}}]) // sum the total of all orders
db.activities.aggregate(([
{ $match: { userId: "u123", ts: { $gt: DATE }}}, // get a time bound set of activities for a user
{ $group: { _id: "$type", count: { $sum: 1 }}}]) // group and sum the results by activity type
db.activity.find({ itemId: “301671", // requires a secondary index on timestamp + itemId
ts: {“$g”t:1301284969946,
“$lt”: 1425657300} })
.sort({ time: -1 }).limit(100)
db.activity.find({ userId: “u123”,
ts: {“$g”t:1301284969946, // time bound the query
“$lt”: 1425657300} })
.sort({ time: -1 }).limit(100) // sort in desc order
"_id" : {
"source" : "user457",
"ts" : NumberLong(324000000)
}
"storage" :
{
"type" : "raw",
"config" :
{
"id_factory" :
{
"type" : "source_time_document",
"config" : { }
}
{
"time_slicing" :
{
"type" : "periodic",
"config" : { "period" : {"weeks" : 4} }
}
}
{ _id: ObjectId(),
geoCode: 1, // used to localize write operations
sessionId: “2373BB…", // tracks activities across sessions
device: { id: “1234", // tracks activities across different user devices
type: "mobile/iphone",
userAgent: "Chrome/34.0.1847.131"
}
type: "VIEW|CART_ADD|CART_REMOVE|ORDER|…", // type of activity
itemId: “301671", // item that was viewed, added to cart, etc.
sku: “730223104376", //item sku
- sh.addTagRange(“inventoryDB.inventory”),{storeId:0},{storeId:100},”west”)
- sh.addTagRange(“inventoryDB.inventory”),{storeId:100},{storeId:200},”central”)
- sh.addTagRange(“inventoryDB.inventory”),{storeId:200},{storeId:300},”east”)
- sh.addShardTag(“shard001”,”west”)
- sh.addShardTag(“shard002”,”central”)
- sh.addShardTag(“shard003”,”east”)