次のテストは、2つの方法で埋め込みドキュメントを更新および削除する方法の例を示しています。(1)標準のアレイアクセサーとメソッドを使用するRuby経由、および(2)DB上の埋め込みドキュメントの更新と削除のためのMongo(MongoMapper / MongoDB)経由サーバ
参照:
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
'。'を含むハッシュキーに注意してください。古い(=>)構文を使用する必要があります。これがお役に立てば幸いです。
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
出力
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