2
votes

How do I query for specific fields in a collection produced from mongodb mapReduce?
What should I enter to retrieve only the lastname field in the output collection?

The result should be:
{ "lastname" : "Doe" }

> version()
version: 2.2.2

> db.test.save( { first: "John", last: "Doe" } )

>db.test.find()
{ "_id" : ObjectId("50bc001a8e97247957c6000f"), "first" : "John", "last" : "Doe" }

> db.test.mapReduce(
function() { emit( this._id, {firstname:this.first, lastname:this.last} ) } , function(key, value) { return null; }, {out: {reduce: 'output'}} )

{ "result" : "output", "timeMillis" : 6, "counts" : { "input" : 1, "emit" : 1, "reduce" : 0, "output" : 1 }, "ok" : 1, }

>db.output.find()
{ "_id" : ObjectId("50bc001a8e97247957c6000f"), "value" : { "firstname" : "John", "lastname" : "Doe" } }

>db.output.find( {}, {_id:0} )
{ "value" : { "firstname" : "John", "lastname" : "Doe" } }

1
I supppose you've already tried >db.output.find( {}, {lastname:1} ) ?Dan Dascalescu
{ "_id" : ObjectId("50bc001a8e97247957c6000f") }pent

1 Answers

0
votes

The closest you could come with a find without reworking your map-reduce would be:

db.output.find( {}, {_id:0, 'value.lastname':1} )