0
我已经mongo_mapper设置像这样:与蒙戈映射删除EmbeddedDocuments
class Person
include MongoMapper::Document
many :pets
end
class Pet
include MongoMapper::EmbeddedDocument
key :animal, String
key :name, String
key :colour, String
end
# Create a person
me = Person.new
# Add pets to the person
me.pets << Pet.new(:animal => 'dog',:name => 'Mr. Woofs', :colour => 'golden')
me.pets << Pet.new(:animal => 'cat', :name => 'Kitty', :colour => 'black')
me.pets << Pet.new(:animal => 'cat', :name => 'Freckles', :colour => 'black')
me.pets << Pet.new(:animal => 'cat', :name => 'Fluffy', :colour => 'tabby')
我知道我可以删除很干脆所有的宠物(me.pets
作品作为数组也回调)
# Delete all pets
me.pets.clear
我也知道我可以这样做删除所有的黑猫:
# Delete black cats
me.pets.delete_if {|pet| pet.animal == 'cat' and pet.colour = 'black'}
但似乎如果需要大量宠物进行迭代,则需要很长时间。
我觉得应该有一种方法只选择黑猫,然后用数组代替clear
。有没有这样的方式?
我很欣赏这个回答,但不幸的是:'NoMethodError:undefined method'all'for []:Array'。我知道像这样的集合在数组上迭代不会花费大量时间,但我期望一次处理10,000个嵌入文档的顺序,并且最多只需要删除约10个! – 2010-08-31 21:00:41
JP,你会花多长时间用你知道截至目前为止最快的方式清空1000个嵌入式文档? – 2011-02-23 04:57:26