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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:30:27+00:00 2026-05-23T21:30:27+00:00

I’ve to add several fields in a mongoid model, I know there is not

  • 0

I’ve to add several fields in a mongoid model, I know there is not migration with MongoDB but if I go on without dropping the DB, making rails to “regenerate” the DB entirely, it doesn’t display or use the new fields at all !

What’s the best way to go here ? Is there something softer than drop/reopen mongodb ?

Thanks in advance
luca

  • 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-23T21:30:28+00:00Added an answer on May 23, 2026 at 9:30 pm

    In general it should be possible to update old documents with the new fields at runtime. There is no need for migrations in MongoDB.

    You maybe want to write rake tasks to update your old documents with the new fields and default values.

    You could find out these documents by checking those new fields which have per default a nil value.


    Update

    Easy style:

    If you define a new field with a default value, this value should always be used as long as you set a new one:

    app/models/my_model.rb

    class MyModel
      include Mongoid::Document
      field :name, type: String
      field :data, type: String
      # NEW FIELD
      field :note, type: String, default: "no note given so far!"
    end
    

    If you query your database you should get your default value for documents which haven’t this field before your extension:

    (rails console)

    MyModel.first
    #=> #<MyModel …other fields…, note: "no note given so far!">
    

    I tested this with a fresh rails stack with a current mongoid on Ruby 1.9.2 – should work with other stacks, too.

    More complicated/complex style:

    If you didn’t set a default value, you’ll get nil for this new field.

    app/models/my_model.rb

    class MyModel
      include Mongoid::Document
      field :name, type: String
      field :data, type: String
      # NEW FIELD
      field :note, type: String
    end
    

    (rails console)

    MyModel.first
    #=> #<MyModel …other fields…, note: nil>
    

    Then you could set up a rake task and migration file like in this example:

    lib/tasks/my_model_migration.rake:

    namespace :mymodel do
      desc "MyModel migration task"
      task :migrate => :environment do
        require "./db/migrate.rb"
      end
    end
    

    db/migrate.rb:

    olds = MyModel.where(note: nil)
    # Enumerator of documents without a valid :note field (= nil)
    olds.each do |doc|
      doc.note = "(migration) no note given yet"
      # or whatever your desired default value should be
      doc.save! rescue puts "Could not modify doc #{doc.id}/#{doc.name}"
      # the rescue is only a failsafe statement if something goes wrong
    end
    

    Run this migration with rake mymodel:migrate.

    This is only a starting point and you can extend this to a full mongoid migration engine.

    The task :migrate => :environment do … is necessary, otherwise rake won’t load models.

    • 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 want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

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.