我成功使用了MongoMapper对原子“$ push”和“$ set”的内置支持,但无法弄清楚“$ pull”
class Feed
include MongoMapper::Document
many :stories
end
class Story
include MongoMapper::EmbeddedDocument
key :title, String
end
feed = Feed.first
story = Story.new(:title => "Hello, world!")
Feed.push(feed.id, :stories => story.to_mongo) #works fine
Feed.set({:_id => feed.id, "stories.title" => "Hello, world!"}, "stories.$.title" => "Foo") #works fine
Feed.pull( ??? )
如何使用pull自动删除故事?
答案 0 :(得分:7)
要使用父ID和子ID自动删除嵌入文档,您可以执行以下操作:
Feed.pull(feed.id, :stories => {:_id => story.id})
如果您已有父文档,则可以改为:
feed.pull(:stories => {:_id => story.id})
现在我对提出这个问题感到尴尬(并回答它)。这非常简单。