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

  • Home
  • SEARCH
  • 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 8369753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:43:56+00:00 2026-06-09T13:43:56+00:00

I’ve run into some strange differences between the mongodb running on MongoHQ and the

  • 0

I’ve run into some strange differences between the mongodb running on MongoHQ and the version running on my own development machine. Specifically, when calling .toString() on an object id inside a MapReduce map function, the results vary:

On my own machine:

ObjectId('foo').toString()  // => 'foo'

On MongoHQ:

ObjectId('foo').toString()  // => 'ObjectId(\'foo\')'

Note: The id’s I use are actual mongodb id’s – not just ‘foo’ etc. as in these examples

I would expect .toString() to behave like on my own machine – not how it’s behaving on MongoHQ. How come it’s not?

My local OSX version of MongoDB is installed using Homebrew and is version 2.0.1-x86_64

To show what’s actually going on, I’ve build a little test case. Let’s assume that we have a users collection with a friends attribute, being an array of user ids:

> db.users.find()
{ _id: ObjectId('a'), friends: [ObjectId('b'), ObjectId('c')] },
{ _id: ObjectId('b'), friends: [] },
{ _id: ObjectId('c'), friends: [] }

As you can see a is friends with b and c where as b and c isn’t friends with anybody.

Now let’s look at a working test-algorithm:

var map = function() {
  this.friends.forEach(function(f) {
    emit(f, { friends: 1, user: user, friend: f.toString() });
  });
};

var reduce = function(k, vals) {
  var result = { friends: 0, user: [], friend: [] };

  vals.forEach(function(val) {
    result.friends += val.friends;
    result.user.push(val.user);
    result.friend.push(val.friend);
  });

  return result;
};

var id = ObjectId('50237c6d5849260996000002');

var query = {
  query   : { friends: id },
  out     : { inline: 1 },
  scope   : { user: id.toString() },
  jsMode  : true,
  verbose : true
};

db.users.mapReduce(map, reduce, query);

Assuming id is set to an id of a user who is a friend of someone in the users collection, then the output returned by the mapReduce method on MongoHQ will look like this:

{
  "results" : [
    {
      "_id" : ObjectId("50237c555849260996000001"),
      "value" : {
        "friends" : 1,
        "user" : "50237c6d5849260996000002",
        "friend" : "ObjectId(\"50237c555849260996000001\")"
      }
    },
    {
      "_id" : ObjectId("50237c74c271be07f6000002"),
      "value" : {
        "friends" : 1,
        "user" : "50237c6d5849260996000002",
        "friend" : "ObjectId(\"50237c74c271be07f6000002\")"
      }
    }
  ],
  "timeMillis" : 0,
  "timing" : {
    "mapTime" : 0,
    "emitLoop" : 0,
    "reduceTime" : 0,
    "mode" : "mixed",
    "total" : 0
  },
  "counts" : {
    "input" : 1,
    "emit" : 2,
    "reduce" : 0,
    "output" : 2
  },
  "ok" : 1,
}

As you can see, the friend attribute in each result is not just a string containing the id, but a string containing the actual method call.

Did I run this on my own machine, the results array would have been:

{
  "_id" : ObjectId("50237c555849260996000001"),
  "value" : {
    "friends" : 1,
    "user" : "50237c6d5849260996000002",
    "friend" : "50237c555849260996000001"
  }
},
{
  "_id" : ObjectId("50237c74c271be07f6000002"),
  "value" : {
    "friends" : 1,
    "user" : "50237c6d5849260996000002",
    "friend" : "50237c74c271be07f6000002"
  }
}
  • 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-09T13:43:57+00:00Added an answer on June 9, 2026 at 1:43 pm

    MongoHQ is running a different version of MongoDB than you are.

    To get the behavior of your homebrew version, try changing your map function:

    var map = function() {
      this.friends.forEach(function(f) {
        emit(f, { friends: 1, user: user.str, friend: f.str });
      });
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns 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.