Skip to content

Instantly share code, notes, and snippets.

@trivoallan
Forked from gperr1/mapreduce.js
Last active August 29, 2015 14:10
Show Gist options
  • Save trivoallan/2795e888e29103847ed0 to your computer and use it in GitHub Desktop.
Save trivoallan/2795e888e29103847ed0 to your computer and use it in GitHub Desktop.
/*
* 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': 'mapreduced_users_online'
}
}
);
// Suppression du idClient vide
db.mapreduced_users_online.find({
_id: ''
})
/*
* Index pour l'expiration sur la table temp
*
* @see http://docs.mongodb.org/manual/tutorial/expire-data/
*/
db.mapreduced_users_online.ensureIndex({
"dateCreated": 1
}, {
expireAfterSeconds: 300
});
/*
* Index pour l'unicité de idClient sur la nouvelle table users_)online
*
* @see http://stackoverflow.com/questions/14184099/fastest-way-to-remove-duplicate-documents-in-mongodb
*/
db.users_online.ensureIndex({
idClient: 1
}, {
unique: true,
dropDups: true
})
/*
* Index pour l'expiration sur la nouvelle table users_online
*
* @see http://docs.mongodb.org/manual/tutorial/expire-data/
*/
db.users_online.ensureIndex({
"dateCreated": 1
}, {
expireAfterSeconds: 300
});
/*
* On copie la table mapreduced dans la collection users_online - l'insertion automatique de _id par mongo
* rend la collection utilisable par Meteor
*/
db.mapreduced_users_online.find().forEach(function(item) {
db.users_online.insert({
idClient: item._id,
user_id: item.value.user_id,
dateCreated: item.value.dateCreated
});
});
// Et normalement là il insère que les nouveaux clients (vu qu'il a déjà réduit les résultats) il ne devrait insérer que si le client n'existe pas déjà dans la table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment