我成功地使用了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( ??? )
如何使用拉动原子地删除故事?
要使用父ID和子ID以原子方式删除嵌入的文档,您可以执行以下操作:
Feed.pull(feed.id, :stories => {:_id => story.id})
如果您已拥有父文档,则可以执行以下操作:
feed.pull(:stories => {:_id => story.id})
现在我对提出这个问题感到尴尬(并回答它).这非常简单.