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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:57:52+00:00 2026-06-09T16:57:52+00:00

I have a problem with a python-script witch i use to aggregate large collections

  • 0

I have a problem with a python-script witch i use to aggregate large collections into smaller pieces and have them grouped by a timestamp.

map = Code("function(number) {"
    "emit({"
        "ts : new Date(new Date((this.ts - (this.ts % (60 * number))) * 1000).setSeconds(0))"
   "}, 1);"
"}")

reduce = Code("function(key, vals) {"
    "var sum = 0;"
    "for (var i in vals) {"
        "sum += vals[i]"
    "}"
    "return sum;"
"}")

As you can see, it’s a pretty simple MapReduce and the timestamp (ts) should be grouped by anny given number of minutes. I’ve tested the Javascript here http://jsfiddle.net/QgMzK/1/ and it seems to work fine. But when i run it in Python all timestamps become ISODate(“1970-01-01T00:00:00Z”).

Any ideas?

  • 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-09T16:57:54+00:00Added an answer on June 9, 2026 at 4:57 pm

    Your map function takes one parameter: number, which, when called by map-reduce, will be set to null and after coertion (and some % by zero) will make the date your map is returning ISODate("0NaN-NaN-NaNTNaN:NaN:NaNZ"). This after type conversion becomes datetime.datetime(1970, 1, 1, 0, 0).

    Remove the parameter and it should work.

    edit

    To confirm that, try running this code:

    from pymongo import Connection
    from bson.code import Code
    
    db = Connection().mr_test
    for i in xrange(10):
        db.things.insert({"x" : i})
    
    
    map = Code("function(number) {"
        "emit({"
            "ts : number"
        "}, 1);"
    "}")
    
    reduce = Code("function(key, vals) {"
        "var sum = 0;"
        "for (var i in vals) {"
            "sum += vals[i]"
        "}"
        "return sum;"
    "}")
    
    
    result = db.things.map_reduce(map, reduce, "test_results")
    for doc in result.find():
        print doc
    

    The result on my machine is :

    {u'_id': {u'ts': None}, u'value': 10.0}
    

    Note that ts is None in the result, cause number was not set when the mapping function executed.

    edit 2

    AFAIK the only way to pass a parameter to map is by using scope optional parameter in map_reduce, but then you’ll have to remove it from map signature anyway.

    So by changing your map to:

    map = Code("function() {"
        "emit({"
            "ts : new Date(new Date((this.ts - (this.ts % (60 * number))) * 1000).setSeconds(0))"
        "}, 1);"
    "}")
    

    and by calling:

    db.whatever.map_reduce(map, reduce, "collection_name", scope = {"number" : the_value_your_function_needs}) 
    

    you can get the result you want.

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

Sidebar

Related Questions

I have a new problem with a python script. When I try to run
I have problem with python multithreaded Queues. I have this script, where producer take
Folks, I have a problem. I am a writing a script in python which
I have a problem trying to turn my python code into an executable using
i have a problem with python script and javascript script in a website. I
I have problem concerning python packages and testing. I'm writing an application using wx
I have a problem in Python. How can I increment by one fully automatically
I have a problem of upgrading python from 2.4 to 2.6: I have CentOS
I have a problem with my python code. I am writing a GUI in
I have a problem automagically detecting my Arduino's serial port in Python, using Mac/Linux.

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.