How to remove an embedded document in MongoMapper using the Pull atom?

I successfully use the built-in MongoMapper support for atomic "$ push" and "$ set", but I can not understand "$ 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( ??? )

How can I remotely delete history using pull?

+3
source share
1 answer

To atomically delete an inline document using only the parent id and child identifier, you can do this:

Feed.pull(feed.id, :stories => {:_id => story.id})

If you already have a parent document, you can do this instead:

feed.pull(:stories => {:_id => story.id}) 

Now I feel awkward asking a question (and answering it). It is pretty simple.

+7
source

Source: https://habr.com/ru/post/1773349/


All Articles