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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:01:02+00:00 2026-06-01T14:01:02+00:00

Suppose I’m working on a MongoMapper class that looks like this: class Animal include

  • 0

Suppose I’m working on a MongoMapper class that looks like this:

class Animal
    include MongoMapper::Document

    key :type, String, :required => true
    key :color, String
    key :feet, Integer
end

Now I want to store a bird’s wingspan. Would it be better to add this, even though it’s irrelevant for many documents and feels a bit untidy:

    key :wingspan, Float

Or this, even though it’s an indescriptive catch-all that feels like a hack:

    key :metadata, Hash

It seems like the :metadata approach (for which there’s precedent in the code I’m inheriting) is almost redundant to the Mongo document as a whole: they’re both intended to be schemaless buckets of key-value pairs.

However, it also seems like adding animal-specific keys is a slippery slope to a pretty ugly model.

Any alternatives (create a Bird subclass)?

  • 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-01T14:01:04+00:00Added an answer on June 1, 2026 at 2:01 pm

    MongoMapper doesn’t store keys that are nil, so if you did define key :wingspan only the documents that actually set that key would store it.

    If you opt not to define the key, you can still set/get it with my_bird[:wingspan] = 23. (The [] call will actually automatically define a key for you; similarly if a doc comes back from MongoDB with a key that’s not explicitly defined a key will be defined for it and all docs of that class–it’s kind of a bug to define it for the whole class but since nil keys aren’t stored it’s not so much of a problem.)

    If bird has its own behavior as well (it probably does), then a subclass makes sense. For birds and animals I would take this route, since every bird is an animal. MongoDB is much nicer than ActiveRecord for Single Table/Single Collection Inheritance, because you don’t need a billion migrations and your code makes it clear which attributes go with which classes.

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

Sidebar

Related Questions

Suppose I have a class that looks like this: class Derived : // some
Suppose I have a data frame, df, that looks like: f t1 t2 t3
Suppose your git history looks like this: 1 2 3 4 5 1–5 are
Suppose i have an XML file, that i use as local database, like this):
Suppose I have some code like this: public string SomeMethod(int Parameter) { string TheString
Suppose I have string Name and Image Photo as properties of a class in
Suppose I have a grid of squared defined like so in a class: Square
Suppose I have a custom object set up in a class similar to this.
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have a FruitDetector class which takes in a string and returns the

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.