In MongoDB, we can manually delete a document from a collection in the selected database using db.collection_name.remove() method.
- Type the command use DATABASE_NAME in the command prompt to create a new database or to select the already created database with the respective name.
For Example: use Hellodb
- Type the command createCollection(name, options) in the command prompt to create a new collection manually.
For Example: db.createCollection(“HELLO”)
- Type the command Collection_Name.insert(Document_name) to insert a document into the already created collection.
For Example: db.HELLO.insert ({“name”: “welcome”})
- For automatic creation of the collection directly insert a document into a collection with the desired name using the Collection_Name.insert(Document_name) command.
For Example: db.HELLO.insert ({“name”: “welcome”})
- Type the command collection_name.find() in the command prompt to see the inserted document in a collection.
For Example: db.HELLO.find()
- Type the command collection_name.remove({}) in the command prompt to remove all the documents from a collection.
For Example: db.HELLO.remove({})
- Type the command collection_name.remove({type : “type_name”}) in the command prompt to remove all the documents from a collection that match the type.
For Example: db.HELLO.remove({type : “SQL”})
- Type the command collection_name.remove({type : “type_name”}, 1) in the command prompt to remove a single document from a collection that match the type.
For Example: db.HELLO.remove({type : “SQL”}, 1)
- Type the command collection_name.find() in the command prompt to check the remaining documents in the collection.
For Example: db.HELLO.find()