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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:53:10+00:00 2026-05-15T17:53:10+00:00

When I’m adding a record in mongodb I can specify whatever keys I want

  • 0

When I’m adding a record in mongodb I can specify whatever keys I want and it will store it in the db. The problem is that it will remember those keys for the next time I insert another record. so for example if I do the following:

Product.create :foo => 123

and then

Product.create :bar => 456

I get :foo => nil field in the 2nd record.

This is definitely not a limitation of mongodb itself, since if I restart the rails console and create yet another record with different set of columns, it will not add the columns from the 1st 2 records.

So it seems like mongomapper remembers all the keys used and inserts them all into all records, even if values are not provided.

The question is obviously: how do I disable this crazy attributes explosion?

Basically I want only the ‘permanent’ keys that I specify in the model to be in every record, but all the ‘extra’ attributes to be specified per record and not to mess the consequent records.

  • 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-15T17:53:11+00:00Added an answer on May 15, 2026 at 5:53 pm

    When you write to a key, MongoMapper will ensure that they key is defined on the document (as if you had declared it yourself with the key class method). See that code here:

    http://github.com/jnunemaker/mongomapper/blob/master/lib/mongo_mapper/plugins/keys.rb#L237

    I don’t think MongoMapper has any way to distinguish between keys you declared in the class yourself and keys it creates dynamically.

    That said, you could always make your own “reset_keys!” method that would destroy the @keys variable in your class and rebuild it again. This is kind of gross, and fragile since we’re breaking encapsulation. Here’s what it might look like!

    class Product
      include MongoMapper::Document
    
      def self.reset_keys!
        @keys = nil
        key :_id, ObjectId
        key :permanent_biz
        key :permanent_buz
      end
    
      reset_keys!
    end
    
    Product.create :foo => 123
    Product.reset_keys!
    Product.create :bar => 456
    

    I didn’t test this, but what could possibly go wrong?

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

Sidebar

Ask A Question

Stats

  • Questions 458k
  • Answers 458k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, I can see three possibilities: X is a schema,… May 15, 2026 at 11:05 pm
  • Editorial Team
    Editorial Team added an answer So for anyone curious this appears to be the solution… May 15, 2026 at 11:05 pm
  • Editorial Team
    Editorial Team added an answer TAPI (Telephony Application Programming Interface) hasn't really been updated since… May 15, 2026 at 11:05 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.