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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:26:00+00:00 2026-06-07T05:26:00+00:00

I can’t seem to figure out how to update or destroy EmbeddedDocuments in MongoMapper.

  • 0

I can’t seem to figure out how to update or destroy EmbeddedDocuments in MongoMapper. Through Googling I’ve seen different methods like delete_if {} and others, but none seem to work for me semantically. Is there anything built-in to MongoMapper to help with this?

class Schedule
  include MongoMapper::Document

  key :name, String
  key :description, String
  key :active, Boolean, :default => false

  many :periods

  validates_presence_of :name

  def activate!
    set({:active => true}, :active => false)

    self.active = true
  end
end

class Period
  include MongoMapper::EmbeddedDocument

  key :number, Integer
  key :text, String
  key :start, Time
  key :finish, Time

  embedded_in :schedule

  before_validation :number!

  validates_presence_of :number
  validates_presence_of :text
  validates_presence_of :start
  validates_presence_of :finish

  validates_format_of :start, :with => /^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$/
  validates_format_of :finish, :with => /^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$/

  def number!
    unless self.number
      number = schedule.periods.count + 1
    end
  end
end
  • 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-07T05:26:01+00:00Added an answer on June 7, 2026 at 5:26 am

    The follow test shows examples on how to update and remove embedded documents in two ways:
    (1) via Ruby using standard Array accessors and methods, and
    (2) via Mongo (MongoMapper / MongoDB) for update and removal of embedded documents on the DB server

    References:

    http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-delete_if
    http://mongomapper.com/documentation/plugins/modifiers.html#set
    http://www.mongodb.org/display/DOCS/Updating#Updating-%24set
    

    Note that Hash keys containing ‘.’ require use of the old ( => ) syntax. Hope that this helps.

    test/unit/period_test.rb

    require 'test_helper'
    
    def ppp(obj)
      puts obj.inspect.gsub(/(, |\[)#/, "\\1\n #")
    end
    
    class PeriodTest < ActiveSupport::TestCase
      def setup
        Schedule.delete_all
        @today = Time.now.midnight
      end
    
      test "update and remove embedded documents" do
        schedule = Schedule.create(name: 'George')
        assert_equal(1, Schedule.count)
        schedule.periods << Period.new(number: 1, text: 'period 1', start: @today + 8.hours, finish: @today + 9.hours)
        schedule.periods << Period.new(number: 2, text: 'period 2', start: @today + 9.hours, finish: @today + 10.hours)
        schedule.periods << Period.new(number: 3, text: 'period 3', start: @today + 10.hours, finish: @today + 11.hours)
        schedule.periods.last.finish = @today + 12.hours #update in Ruby
        schedule.save
        assert_equal(1, Schedule.count)
        puts "schedule with three periods --------"
        ppp Schedule.first
        schedule.periods.delete_if {|period| period.text == 'period 2'} #remove in Ruby
        schedule.save
        puts "schedule after removing period 2 in Ruby --------"
        ppp Schedule.first
        Schedule.set( {name: 'George', 'periods.text' => 'period 1'}, {'periods.$.finish' => @today + 10.hours} ) #update embedded document in MongoDB
        puts "schedule after updatting period 1 via Mongo --------"
        ppp Schedule.first
        Schedule.pull( {name: 'George'}, { periods: { text: 'period 1'} } ) #remove embedded document in MongoDB
        puts "schedule after pulling period 1 via Mongo --------"
        ppp Schedule.first
      end
    end
    

    output

    Run options: --name=test_update_and_remove_embedded_documents
    
    # Running tests:
    
    schedule with three periods --------
    #<Schedule _id: BSON::ObjectId('4ff732e77f11ba6fe9000001'), active: false, name: "George", periods: [
     #<Period _id: BSON::ObjectId('4ff732e77f11ba6fe9000002'), finish: Fri, 06 Jul 2012 13:00:00 UTC +00:00, number: 1, start: Fri, 06 Jul 2012 12:00:00 UTC +00:00, text: "period 1">,
     #<Period _id: BSON::ObjectId('4ff732e77f11ba6fe9000003'), finish: Fri, 06 Jul 2012 14:00:00 UTC +00:00, number: 2, start: Fri, 06 Jul 2012 13:00:00 UTC +00:00, text: "period 2">,
     #<Period _id: BSON::ObjectId('4ff732e77f11ba6fe9000004'), finish: Fri, 06 Jul 2012 16:00:00 UTC +00:00, number: 3, start: Fri, 06 Jul 2012 14:00:00 UTC +00:00, text: "period 3">]>
    schedule after removing period 2 in Ruby --------
    #<Schedule _id: BSON::ObjectId('4ff732e77f11ba6fe9000001'), active: false, name: "George", periods: [
     #<Period _id: BSON::ObjectId('4ff732e77f11ba6fe9000002'), finish: Fri, 06 Jul 2012 13:00:00 UTC +00:00, number: 1, start: Fri, 06 Jul 2012 12:00:00 UTC +00:00, text: "period 1">,
     #<Period _id: BSON::ObjectId('4ff732e77f11ba6fe9000004'), finish: Fri, 06 Jul 2012 16:00:00 UTC +00:00, number: 3, start: Fri, 06 Jul 2012 14:00:00 UTC +00:00, text: "period 3">]>
    schedule after updatting period 1 via Mongo --------
    #<Schedule _id: BSON::ObjectId('4ff732e77f11ba6fe9000001'), active: false, name: "George", periods: [
     #<Period _id: BSON::ObjectId('4ff732e77f11ba6fe9000002'), finish: Fri, 06 Jul 2012 14:00:00 UTC +00:00, number: 1, start: Fri, 06 Jul 2012 12:00:00 UTC +00:00, text: "period 1">,
     #<Period _id: BSON::ObjectId('4ff732e77f11ba6fe9000004'), finish: Fri, 06 Jul 2012 16:00:00 UTC +00:00, number: 3, start: Fri, 06 Jul 2012 14:00:00 UTC +00:00, text: "period 3">]>
    schedule after pulling period 1 via Mongo --------
    #<Schedule _id: BSON::ObjectId('4ff732e77f11ba6fe9000001'), active: false, name: "George", periods: [
     #<Period _id: BSON::ObjectId('4ff732e77f11ba6fe9000004'), finish: Fri, 06 Jul 2012 16:00:00 UTC +00:00, number: 3, start: Fri, 06 Jul 2012 14:00:00 UTC +00:00, text: "period 3">]>
    .
    
    Finished tests in 0.038952s, 25.6726 tests/s, 51.3452 assertions/s.
    
    1 tests, 2 assertions, 0 failures, 0 errors, 0 skips
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can't figure out how to do this in a pretty way : I have
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can I run this in a Windows command prompt like I can run it
Can't work out a way to make an array of buttons in android. This
Can anyone help me trying to find out why this doesn't work. The brushes
Can't seem to get the Back Button to appear in a UINavigationController flow. I
can anybody confirm what are the currently allowed methods for peer-to-peer communications within the
Can you cast a List<int> to List<string> somehow? I know I could loop through
Can anyone tell me how I can display a status message like 12 seconds

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.