Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8682505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:47:42+00:00 2026-06-12T21:47:42+00:00

It was my understanding that when you destroy a parent document in Mongo that

  • 0

It was my understanding that when you destroy a parent document in Mongo that you also destroy its children and it will cascade down the chain until all referenced documents have been removed.

I have a collection structure like the following

class A
    include Mongoid::Document
    field :name, :type => String
    has_many :bs
end

class B
    include Mongoid::Document
    field :name, :type => String
    has_many :cs
end

class C
    include Mongoid::Document
    field :name, :type => String
end

I came across a situation in my code where I needed to delete one of Class A and all of its relevant documents. Since Each of these models were based of Mongoid I used the destroy_all method like so

a = A.where({'_id' => "123456789"})
a.bs.destroy_all
 => 'however many a's I had'

From reading the documentation I thought that each of the referenced documents would be removed aswell.

Unfortunately what has happened is all my class b’s are gone and I have a bunch of orphaned class c’s in my database.

So:

A) Assuming destroy_all doesn’t do what I thought it would. Is there anything that can be used to actually delete a parent and all of its referenced documents in mongoid?

B) Although I performed this operation on a local machine, I would still like to know, is there any way to remove orphan documents from an altered collection?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:47:43+00:00Added an answer on June 12, 2026 at 9:47 pm

    It was my understanding that when you destroy a parent document in Mongo that you also destroy its children

    Only when it is a single document. You are showing a structure of many documents.

    I am not a Ruby programmer and I have never used mongoid however it seems that destroy_all is essentially remove that matches more than one document as is supported by the docs: http://two.mongoid.org/docs/persistence/standard.html#destroy_all

    Deletes all matching documents in the database given the supplied
    conditions. See the criteria section on deletion for preferred ways to
    perform these actions. This runs destroy callbacks on all matching
    documents.

    I am guessing that if you wish to remove the children as well you will be required to manually specify them since MongoDB has no relational behaviour and so has no ability to cascade your “relations” on its own.

    The only real way, I would say, to remove orphaned documents is most likely the hard way by going through all distinct references to the documents parents within the child collection querying the parent collection to see if it exists. If it does not exist, remove it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's my understanding that in Spring, all objects are treated by default as singletons.
It's my understanding that all three of these lines below should return an ARRAY
It's my understanding that all .NET object instances begin with an 8 byte 'object
In my understanding a singleton object will destroy only when the application is about
Its my understanding that the questions in StackOverflow has the following format http://stackoverflow.com/questions/{question-id}/{slug-made-from-question-title} So
It is my understanding that a display:block will automatically start in a new line.
Understanding that I should probably just dig into the source to come up with
It is my understanding that two unequal objects can have the same hashcode. How
It is my understanding that a texture atlas is basically a single texture that
So it's my understanding that on a ReaderWriterLock (or ReaderWriterLockSlim more specifically), both the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.