Skip to content

Instantly share code, notes, and snippets.

@naholyr
Created January 9, 2011 22:25
Show Gist options
  • Save naholyr/772094 to your computer and use it in GitHub Desktop.
Save naholyr/772094 to your computer and use it in GitHub Desktop.
Liste des auteurs triés par nombre de livre
m = function() {
this.nb_books = this.books.length;
emit(this._id, this);
}
r = function(k,v) {
return v[0];
}
db[db.authors.mapReduce(m, r).result].find().sort({nb_books:-1})
m = function() {
emit(this._id, this.books.length);
}
r = function(k,v) {
var s=0;
for (var i=0;i<v.length;i++) s+=v[i];
return s;
}
db[db.authors.mapReduce(m, r).result].find().sort({value:-1})
@naholyr
Copy link
Author

naholyr commented Jan 9, 2011

Exemple de sortie :

{ "_id" : ObjectId("4d29b104b75cca91684e7f1b"), "value" : 1 }
{ "_id" : ObjectId("4d29b0f6b75cca91684e7f1a"), "value" : 2 }

Il faut encore convertir ça en objets complets.

@naholyr
Copy link
Author

naholyr commented Jan 9, 2011

Dans la version full, on a ce type de sortie :

{ "_id" : ObjectId("4d29b0f6b75cca91684e7f1a"), "value" : { "_id" : ObjectId("4d29b0f6b75cca91684e7f1a"), "name" : "Bill", "birth" : "Tue Jan -2147483647 584555983 15:25:52 GMT+0100 (CET)", "books" : [
    ObjectId("4d29b086b75cca91684e7f19"),
    ObjectId("4d29b086b75cca91684e7f19")
], "nb_books" : 2 } }
{ "_id" : ObjectId("4d29b104b75cca91684e7f1b"), "value" : { "_id" : ObjectId("4d29b104b75cca91684e7f1b"), "name" : "Bill's wife", "birth" : "Tue Jan -2147483647 584555987 15:25:52 GMT+0100 (CET)", "books" : [ ObjectId("4d29b086b75cca91684e7f19") ], "nb_books" : 1 } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment