DEV Community

Cover image for CRUD MongoDB
Masudur Rahman
Masudur Rahman

Posted on

CRUD MongoDB

CRUD

In computer programming, create , read , update , and delete are the four basic operations of persistent storage. CRUD is also sometimes used to describe user interface conventions that facilitate viewing , searching , and changing information using computer-based forms and reports.

To create a database in MongoDB, we have to start by creating a MongoClient object , then specify a connection URL with the correct *ip * address and the name of the database we want to create.

MongoDB will create the database if it does not exist, and make a connection to it. For example-

 var MongoClient = require('mongodb').MongoClient;
 var url = "mongodb://localhost:27017/mydb";

 MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    console.log("Database created!");
    db.close();
 });
Enter fullscreen mode Exit fullscreen mode

Second Step Collection

To create a collection in MongoDB, use the createCollection() method:

Create a collection called "customers":
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydb");
  dbo.createCollection("customers", function(err, res) {
    if (err) throw err;
    console.log("Collection created!");
    db.close();
  });
});
Enter fullscreen mode Exit fullscreen mode

Create

The first letter of CRUD , ‘C’ , refers to CREATE aka add , insert . Now insert a document in the "customers" collection.To insert a record, or document as it is called in MongoDB, into a collection, we use the insertOne() method.

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydb");
  var myobj = { name: "Company Inc", address: "Highway 37" };
  dbo.collection("customers").insertOne(myobj, function(err, res) {
    if (err) throw err;
    console.log("1 document inserted");
    db.close();
  });
});
Enter fullscreen mode Exit fullscreen mode

Find One

To select data from a collection in MongoDB, we can use the findOne() method. The findOne() method returns the first occurrence in the selection. The first parameter of the findOne() method is a query object. In this example we use an empty query object, which selects all documents in a collection (but returns only the first document).

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydb");
  dbo.collection("customers").findOne({}, function(err, result) {
    if (err) throw err;
    console.log(result.name);
    db.close();
  });
});
Enter fullscreen mode Exit fullscreen mode

Delete Document

To delete a record, or document as it is called in MongoDB, we use the deleteOne() method. The first parameter of the deleteOne() method is a query object defining which document to delete.

Delete the document with the address "Mountain 21":

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydb");
  var myquery = { address: 'Mountain 21' };
  dbo.collection("customers").deleteOne(myquery, function(err, obj) {
    if (err) throw err;
    console.log("1 document deleted");
    db.close();
  });
});
Enter fullscreen mode Exit fullscreen mode

Update Document

We can update a record, or document as it is called in MongoDB, by using the updateOne() method. The first parameter of the updateOne() method is a query object defining which document to update.

Update the document with the address "Valley 345" to name="Mickey" and address="Canyon 123"

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://127.0.0.1:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydb");
  var myquery = { address: "Valley 345" };
  var newvalues = { $set: {name: "Mickey", address: "Canyon 123" } };
  dbo.collection("customers").updateOne(myquery, newvalues, function(err, res) {
    if (err) throw err;
    console.log("1 document updated");
    db.close();
  });
});
Enter fullscreen mode Exit fullscreen mode

Query

Filter the Result
When finding documents in a collection, we can filter the result by using a query object. The first argument of the find() method is a query object, and is used to limit the search.

Find documents with the address "Park Lane 38"

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydb");
  var query = { address: "Park Lane 38" };
  dbo.collection("customers").find(query).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)