Last active
August 29, 2015 14:10
-
-
Save trivoallan/24c25d7f0992d9ddec4f to your computer and use it in GitHub Desktop.
mapreduce.js
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
/* | |
* Aggrégation des données | |
*/ | |
db.request.mapReduce( | |
function() { | |
key = this.idClient, | |
value = { | |
user_id : this.tagCommanderData.user_id, | |
dateCreated : this.dateCreated | |
}; | |
emit(key, value); | |
}, | |
function(key, values) { | |
var tempVal = { | |
user_id : '', | |
dateCreated : '' | |
}; | |
for (var idx = 0; idx < values.length; idx++) { | |
if( values[idx].user_id != '' && tempVal.user_id == '') { | |
tempVal.user_id = values[idx].user_id; | |
} | |
if(tempVal.dateCreated == '' || tempVal.dateCreated < values[idx].dateCreated) { | |
tempVal.dateCreated = values[idx].dateCreated; | |
} | |
} | |
return tempVal; | |
}, | |
{ | |
query: {}, | |
out: {'merge' : 'users_online'} | |
} | |
); | |
/* | |
* Création de l'index pour l'expiration | |
* | |
* @see http://docs.mongodb.org/manual/tutorial/expire-data/ | |
*/ | |
db.users_online.ensureIndex( { "value.dateCreated": 1 }, { expireAfterSeconds: 300 } ) | |
/* | |
* Sélection des données aggrégées et non expirées | |
*/ | |
db.client_salesforce.count({"value.user_id" : {$ne: "" }}); | |
db.client_salesforce.find({"value.user_id" : {$ne: "" }}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment