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 8460705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:40:54+00:00 2026-06-10T13:40:54+00:00

I’ve found a bunch of map_reduce tutorials around, but none of them seem to

  • 0

I’ve found a bunch of map_reduce tutorials around, but none of them seem to have a “where” clause in them or any other way to exclude documents/records from what’s being considered. I’m working on a seemingly easy query. I have a basic log file of events with timestamps, ip addresses, and campaign ids. I want to get a count of unique users, within a given timestamp range, for a given campaign. Sounds easy!

I built out a query object that is something like this:

{'ts': {'$gt': 1345840456, '$lt': 2345762454}, 'cid': '2636518'}

With that, I’ve tried two things, one using distinct, and the other with map_reduce:

Distinct

db.alpha2.find(query).distinct('ip').count()

In the mongo shell, you can put the query as a second parameter of the distinct function, and it works there, but I’ve read that you can’t do that in pymongo.

Map_reduce

map = Code("function () {"
        "    emit(this.ip, 1);"
        "}")
reduce = Code("function (key, values) {"
    "  var total = 0;"
    "  for (var i = 0; i < values.length; i++) {"
    "    total += values[i];"
    "  }"
    "  return total;"
    "}")

totaluniqueimp = db.alpha2.map_reduce(map, reduce, "myresults").count();

(I realize the reduce function is doing stuff I don’t need, I took it from the demo). This works fine, but makes no use of my “where” paramaters. I try this:

totaluniqueimp = db.alpha2.find(query).map_reduce(map, reduce, "myresults").count();`

And I get this error:

AttributeError: 'Cursor' object has no attribute 'map_reduce'

Conclusion

Basically, this is what I’m trying to do in mysql:

select count(*) from records where ts<1000 and ts>900 and campaignid=234 group by ipaddress

It seems so simple! How do you do this in mongo?

UPDATE: ANSWER

Based off of Dmitry’s answer below, I was able to solve (and simplify) my solution to (is this as simple as I can make it?):

#query is an object that was built above this
map = Code("function () { emit(this.ip, 1);}")
reduce = Code("function (key, values) {return 1;}")
totaluniqueimp = collection.map_reduce(map, reduce, "myresults", query=query).count();

Thanks Dmitry!

  • 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-06-10T13:40:56+00:00Added an answer on June 10, 2026 at 1:40 pm

    You could try using this:

    totaluniqueimp = db.alpha2.map_reduce(map, reduce, {
        out: "myresults",
        query: {'ts': {'$gt': 1345840456, '$lt': 2345762454}, 'cid': '2636518'}
    }).count();
    

    UPDATE: the statement above works in the mongo shell. In pymongo you should add the query as the fourth parameter:

    totaluniqueimp = db.alpha2.map_reduce(map, reduce, "myresults", query={'ts': {'$gt': 1345840456, '$lt': 2345762454}, 'cid': '2636518'})
    

    The detailed documentation can be found here.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't

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.