Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5993049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:37:11+00:00 2026-05-22T23:37:11+00:00

I have a collection with entries that look like that : {userid: 1, contents:

  • 0

I have a collection with entries that look like that :

{“userid”: 1, “contents”: [ { “tag”: “whatever”, “value”: 100 }, {“tag”: “whatever2”, “value”: 110 } ] }

I’m performing a MapReduce on this collection with queries such as {“contents.tag”: “whatever”}.

What I’d like to do in my map function is emiting the field “value” corresponding to the entry in the array “contents” that matched the query without having to iterate through the whole array. Under normal circumstances, I could do that using the $ positional operator with something like contents.$.value. But in the MapReduce case, it’s not working.

To summarize, here is the code I have right now :`

map=function(){
        emit(this.userid, WHAT DO I WRITE HERE TO EMIT THE VALUE I WANT ?);
    }
    reduce=function(key,values){
        return values[0]; //this reduce function does not make sense, just for the example
    }
    res=db.runCommand(
    {
        "mapreduce": "collection",
        "query": {'contents.tag':'whatever'},
        "map": map,
        "reduce": reduce,
        "out": "test_mr" 
    }
    );`

Any idea ?

Thanks !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:37:12+00:00Added an answer on May 22, 2026 at 11:37 pm

    This will not work without iterating over the whole array. In MongoDB a query is intended to match an entire document.

    When dealing with Map / Reduce, the query is simply trimming the number of documents that are passed into the map function. However, the map function has no knowledge of the query that was run. The two are disconnected.

    The source code around the M/R is here.

    There is an upcoming aggregation feature that will more closely match this desire. But there’s no timeline on this feature.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection with entries that look like : { userid: 1, contents:
for example I have collection foo of documents like that: {tag_cloud:[{value:games, count:10}, {value:girls, count:500}]}
I have a MongoDB Collection called Questions which holds JSON Objects that look like
I have a table that is a collection entries as to when a user
I'm using Devise. I'm updating a collection of Log Entries. My routes look like:
Save that I have a (large) collection of instances of Dictionary. The key value
I have a domain collection type called SavedAnswers and would like to add that
I have a collection that use as a cache. How can I limit the
I have a collection of Objective-C classes that get sub-classed at a variety of
I have a self-referencing many-to-many relationship between users and managers that looks like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.