2

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" } }

2
  • I supppose you've already tried >db.output.find( {}, {lastname:1} ) ? Commented Dec 3, 2012 at 1:41
  • { "_id" : ObjectId("50bc001a8e97247957c6000f") } Commented Dec 3, 2012 at 1:58

1 Answer 1

0

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

db.output.find( {}, {_id:0, 'value.lastname':1} )
Sign up to request clarification or add additional context in comments.

4 Comments

{ "value" : { "lastname" : "Doe" } }
What would be another way to rework the map-reduce to produce those results?
@pent What are you actually trying to do with this? It's sort of a nonsensical map-reduce in its current form.
@pent I was thinking there was a way to flatten the output to remove the value level but it appears there isn't: jira.mongodb.org/browse/SERVER-2517

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.