• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:9 mins read
You are currently viewing MongoDB Remove File from the Document

How to remove file from the document in the MongoDB collection? MongoDB can store files as binary data directly in a document or using GridFS, which is a file storage system built on top of MongoDB. Thus, we can remove these files by using different approaches of MongoDB.

Advertisements

In this article, we will go over various approaches to removing files from documents in MongoDB. Before that, let’s have a collection student with a file stored in the document like the one below.


# Create Collection
db.student.insertMany([
      {
         "_id": 1,
         "name": "James",
         "profile": "JamesImage.jpg",
         "contentType": "image/jpeg"
      },
      {
         "_id": 2,
         "name": "Kelie",
         "profile": "KelieImage.jpg",
         "contentType": "image/jpeg"
      },
      {
         "_id": 3,
         "name": "Harry",
         "profile": "HarryImage.jpg",
         "contentType": "image/jpeg"
      },
      {
         "_id": 4,
         "name": "Alice",
         "profile": "AliceImage.jpg",
         "contentType": "image/jpeg",
         "assignment" : [{"assignment1" :"MongoDB.txt"}]
      }
   ]
)

1. Using $unset operator to remove file in MongoDB

You can use the $unset operator to remove a file from a MongoDB document by specifying the field that contains the file. For example, we have the update query below where the first argument is specified with the field _id along with the value 1 to be matched. Then, the second argument is set with the $unset operator. The $unset operator is passed with the field profile which contains the file JamesImage.jpg. Here, the $unset operator will remove the entire field, not just the file content.


# Usage of $unset operator to remove file
db.student.update(
  { _id: 1 },
  { $unset: { profile: "JamesImage.jpg" } }
);

The output retrieved below matched the document and modified it by unsetting that document.

Remove file from MongoDB

2. Using $pull operator to remove file in MongoDB

If you have multiple files in an array field then we can use the $pull operator to remove a specific file from the array in MongoDB. Note that, in the above example, the document _id equal to 4 is the only document set with the array field. The array field stored the file there. Now, we are using the $pull operator to remove that file stored in the array field.

For example, we have the following update query where the _id:4 is specified to update the document. Then, the $pull operator is passed with the field assignment which contains the array field assignment1. The assigment1 has a file MongoDB.txt that will be removed.


# Usage of $pull operator 
db.student.update(
   { _id: 4 },
   { $pull: { assignment: 
     { assignment1: "MongoDB.txt" } 
    } }
)

The output indicates the document is matched and updated after removing the file from the array field.

Remove file in MongoDB

3. Using GridFS API to remove file in MongoDB

In addition, if the files are stored using GridFS, we can use the MongoDB GridFS API to remove a file from the database. Note that once we have executed the commands of GridFS API, the file and all of its associated metadata will be permanently deleted from the database.

For this, we have inserted the following metadata document containing information about the file, such as its name, size, and content type in the student.files collection.


# Usage of GridFS API
db.student.files.insert({
  _id: ObjectId("603cc12b19edba1a9432239e"),
  name: "Nina",
  profile: "NinaImage.jpg",
  contentType: "image/jpeg"
})

Then, we stored the meta-document in chunks by using the following insert query on the student.chunks collection. Here, the files_id field in the chunks collection refers to the _id of the metadata document in the files collection that corresponds to the file.


# Storing meta document
db.student.chunks.insert({
  files_id: ObjectId("603cc12b19edba1a9432239e"),
  n: 0,
  data: BinData(0, "")
})

Now, we are using the following query which removes the metadata document for the file with the specified _id object from the student.files collection by using the remove() method.


# Removing the metadata document
db.student.files.remove({ 
  _id: ObjectId("603cc12b19edba1a9432239e") 
})

The output displayed that the meta-document is removed. 

delete file in MongoDB

Next, we use the remove() method on the student.chunks collection to delete the above file chunks.


# Usage of remove() method
db.student.chunks.remove({ files_id: ObjectId("603cc12b19edba1a9432239e") })

The output declared the file is removed. Note that these two queries must be executed together to completely remove a file from a GridFS collection.

Remove file in MongoDB

4. Using remove() method

Alternatively, the remove() method in MongoDB is also used to remove one or more documents containing files from mongoDB collection that matches a specified filter. If the version of MongoDB is older than 4.0 then this method might come in handy to remove a file from a document.

For example, We have the following query where the remove() method is used over the collection student. Then, the remove() method takes the field profile with the file HarrayImage.jpg. The remove() method first finds the specified file in the profile field of every document and removes the particular document entirely.


# Using remove()
db.student.remove({ profile: "HarryImage.jpg"  })

The output below indicates the document that contains the specified file is removed from the collection.

Remove file in MongoDB

More details about the remove() method can be found here.

5. Conclusion

In this article, you have learned different methods to remove files from documents in MongoDB. The method we choose may depend on how we store files in MongoDB and the structure of the documents. You can choose any approve from the above that best fits your need.