How to remove a row in mongodb?

by mallory_cormier , in category: PHP Databases , 2 years ago

How to remove a row in mongodb?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by edmond_brakus , a year ago

@mallory_cormier 

To remove a row, or document, from a MongoDB collection, you can use the deleteOne() or deleteMany() method of the Collection object.


Here's an example of how to use deleteOne() to delete a single document that matches a specific condition:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<username>:<password>@cluster0.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
  const collection = client.db("test").collection("devices");
  // delete the document with the field 'status' set to 'inactive'
  collection.deleteOne({ status: 'inactive' }, function(err, result) {
    console.log("Deleted the document with the field 'status' set to 'inactive'");
    client.close();
  });
});


To delete multiple documents that match a specific condition, you can use the deleteMany() method instead:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<username>:<password>@cluster0.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
  const collection = client.db("test").collection("devices");
  // delete all documents with the field 'status' set to 'inactive'
  collection.deleteMany({ status: 'inactive' }, function(err, result) {
    console.log("Deleted all documents with the field 'status' set to 'inactive'");
    client.close();
  });
});


Keep in mind that the deleteOne() and deleteMany() methods only delete the documents that match the specified condition and do not modify the structure of the collection. To permanently remove the documents and free up the space they occupied on the disk, you can use the compact command.


I hope this helps! Let me know if you have any questions.

Member

by dana , 5 months ago

@mallory_cormier 

To remove a row, or document, in MongoDB, you can use the deleteOne() or deleteMany() methods. These methods are available on the Collection object.


Here's an example on how to remove a row using deleteOne():

  1. Firstly, connect to your MongoDB cluster:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
const { MongoClient } = require('mongodb');

const uri = 'mongodb+srv://<username>:<password>@cluster0.mongodb.net/<database>?retryWrites=true&w=majority';
const client = new MongoClient(uri);

client.connect(err => {
  // handle error, if any
  // perform the delete operation here
  client.close();
});


Replace <username>, <password>, and <database> with your own credentials.

  1. Once connected, specify the collection object you want to remove a row from:
1
const collection = client.db('<database>').collection('<collection>');


Replace <database> with your database name and <collection> with the collection name.

  1. Use the deleteOne() method to remove a single document:
1
2
3
4
5
collection.deleteOne({ <condition> }, function(err, result) {
  // handle error, if any
  console.log('Row deleted');
  client.close();
});


Replace <condition> with the condition that matches the document you want to delete. For example, { name: 'John' } will delete the document with the name 'John'.


If you want to remove multiple documents that match a specific condition, you can use the deleteMany() method instead:

1
2
3
4
5
collection.deleteMany({ <condition> }, function(err, result) {
  // handle error, if any
  console.log('Rows deleted');
  client.close();
});


Remember to replace <condition> with the condition that matches the documents you want to delete.


After executing the delete operation, the documents will be permanently removed from the collection.