Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Mongo DB MapReduce: Emit key from array based on condition
- {
- "_id" : ObjectId("4fc5ed3e67960de6794dd21c"),
- "name" : "some name",
- "uid" : "some app specific uid",
- "collection" : "some name",
- "metadata" : [
- {
- "key" : "key1",
- "value" : "Plain text",
- "status" : "SINGLE_RESULT",
- },
- {
- "key" : "key2",
- "value" : "text/plain",
- "status" : "SINGLE_RESULT",
- },
- {
- "key" : "key3",
- "value" : 3469,
- "status" : "OK",
- }
- ]
- }
- function map() {
- var mime = "";
- this.metadata.forEach(function (m) {
- if (m.key === "key2") {
- mime = m.value;}
- });
- emit(mime, {count:1});
- }
- function reduce() {
- var res = {count:0};
- values.forEach(function (v) {res.count += v.count;});
- return res;
- }
- db.collection.mapReduce(map, reduce, {out: { inline : 1}})
- {
- "_id" : ObjectId("4fc5ed3e67960de6794dd21c"),
- "name" : "some name",
- "uid" : "some app specific uid",
- "collection" : "some name",
- "metadata" : {
- "key1" : {
- "value" : "Plain text",
- "status" : "SINGLE_RESULT"
- },
- "key2": {
- "value" : "text/plain",
- "status" : "SINGLE_RESULT"
- },
- "key3" : {
- "value" : 3469,
- "status" : "OK"
- }
- }
- }
- function map() {
- emit( this.metadata["key2"].value, { count : 1 } );
- }
- function map() {
- var mime = "";
- this.metadata.forEach(function (m) {
- if (m.key === "key2") {
- mime = m.value;
- break;
- }
- });
- emit(mime, {count:1});
- }
Advertisement
Add Comment
Please, Sign In to add comment