What will happen when you remove a document from database in MongoDB? Does MongoDB remove it from disk?

Yes. If you remove a document from database, MongoDB will remove it from disk too.

When you remove a document from a MongoDB database, MongoDB marks the space occupied by the document as free and available for reuse within the database. However, MongoDB does not immediately remove the document data from the disk. Instead, the space is reclaimed through a process called “compaction” during the regular background maintenance tasks performed by MongoDB.

MongoDB uses a process called “storage engine” to manage the storage of data on disk. Different storage engines may behave slightly differently, but in general, the removal of a document results in the space being marked as reusable rather than being immediately freed up.

It’s important to note that the removal of documents and the subsequent space reclamation is handled internally by MongoDB, and from the application’s perspective, the document is considered deleted. The specifics of storage management can depend on factors such as the storage engine in use, configuration settings, and the version of MongoDB being used.