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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:43:10+00:00 2026-05-22T00:43:10+00:00

I am currently playing around with node.js and MongoDB using the node-mongo-native driver. I

  • 0

I am currently playing around with node.js and MongoDB using the node-mongo-native driver.

I tested a bit around using the Mongo console storing and retrieving JS objects. I figured out, that if I store an object that contains functions/methods the methods and functions will also be stored in the collection. This is interesting since I thought that functions could not be stored in MongoDB (with the exception of the system.js collection, as suggested by the Mongo docs).
Also it will not only store the methods but actually each method and member of the object’s entire prototype chain. Besides that I dont like this behaviour and think it’s unintuitive I mustn’t have it.

I was going to manage users in a Mongo collection. To do this I have a User object containing all of the users methods functioning as a prototype for each instance of an user. The user object itself would only contain the users attributes.

If I store a user in the Mongo collection I only want to store the own properties of the user object. No prototype members and especially no prototype methods. Currently I do not see how to cleanly do this. The options that I figured might work are:

  1. creating a shallow copy using foreach and hasOwnProperty and storing this copy in the collection.
  2. Add a data attribute to each user that contains all the object’s attributes and can be stored in the collection.
  3. This just came to my mind writing this: I could also set all the prototypes properties to not enumerable which should prevent them from being stored in the collection.

However, I do have the same issues the other way around: when loading a user from a collection. AFAIK there is no way to change an objects prototype in JavaScript after it was created. And there’s also no way to specify a prototype to use when Mongo instantiates objects it retrieved from a collection. So basically I always get objects that inherit from Object using Mongo. As far as I can tell I have 2 options to restore a usable user object from this point on:

  1. Create a fresh object inheriting from User and copying each attribute on the result object to the newly created object. (Compatible to storing mechanisms 1 & 3)
  2. Create a fresh object inheriting from User and storing the result object as a data attribute on the newly created object. (Compatible to storing mechanism 2)

Are my assumptions, especially about the possibility to specify a prototype for query results, correct? What’s the right way to do it, and why? I’m surely not the first person struggling to store and resurrect objects in/from MongoDB using node.js.

Currently I would go with the approach 2/2. I don’t really like it, but it is the most efficient and the only one that works cleanly with the API. However, I’d much rather hear that actually the API does nothing wrong, but I do for not knowing how to use it correctly. So please, enlighten me 🙂

  • 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-22T00:43:11+00:00Added an answer on May 22, 2026 at 12:43 am

    I just recently realized, that it actually is possible to change an objects prototype in V8/node. While this is not in the standard it is possible in various browsers and especially in V8/node!

    function User(username, email) {
        this.username = username;
        this.email = email;
    }
    
    User.prototype.sendMail = function (subject, text) {
        mailer.send(this.email, subject, text);
    };
    
    var o = {username: 'LoadeFromMongoDB', email: 'nomail@nomail.no'};
    o.__proto__ = User.prototype;
    o.sendMail('Hello, MongoDB User!', 'You where loaded from MongoDB, but inherit from User nevertheless! Congratulations!');
    

    This is used all over various modules and plugins – even core modules make use of this technique, allthough it is not ECMAScript standard. So I guess it is safe to use within node.js.

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

Sidebar

Related Questions

I'm currently playing a bit around with scheme, and I've been stuck for quite
i`m currently playing around with WPF and now i wonder what would be the
I'm currently playing around with HTML_QuickForm for generating forms in PHP. It seems kind
Hallo i am currently playing around with castle projects ActiveRecord and the remoting facility.
I am currently playing around with the HybridSessionBuilder class found on Jeffrey Palermo's blog
I am currently playing around with the Asp.Net mvc framework and loving it compared
I am playing around with Hadoop and have set up a two node cluster
I am currently playing around with the FCKEditor, and I am trying to replicate
I am currently playing around with the idea of having history tables for some
I'm currently playing around with a CMS idea I've got. It's based on a

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.