SoFunction
Updated on 2025-03-09

Operation MongoDB database instance analysis

This article describes the operation of MongoDB database. Share it for your reference, as follows:

Operate MongoDB

npm init
npm i mongodb --save

{
 "name": "test",
 "version": "1.0.0",
 "description": "",
 "main": "",
 "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
 },
 "author": "",
 "license": "ISC",
 "dependencies": {
  "mongodb": "^3.1.1"
 }
}

Connect to the database

// 
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
// Use connect method to connect to the server
(url, { useNewUrlParser: true }, function(err, client) {
 ("Connected successfully to server");
 const db = (dbName);
 ();
});

insert

// 
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
// Insertvar insertData = function (db, callback) {
 // Get the document collection var collection = ('collection3');
 var data = [{"name": "Li Ergou001", "age": 20}, {"name": "Li Ergou002", "age": 21}];
 // Insert the document (data, function (err, result) {
  if(err) {
   ('Error: ' + err);
   return;
  }
  callback(result);
 })
}
// Use connect method to connect to the server
(url, { useNewUrlParser: true }, function(err, client) {
 ("Connected successfully to server");
 const db = (dbName);
 insertData(db, function (result) {
  (result);
  ();
 });
});

Query

// 
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
// Queryvar findData = function (db, callback) {
 // Get the document collection var collection = ('collection3');
 var whereStr = {"name": "Li Ergou001"};
 // Query the document (whereStr).toArray(function (err, result) {
  if(err) {
   ('Error: ' + err);
   return;
  }
  callback(result);
 })
}
// Use connect method to connect to the server
(url, { useNewUrlParser: true }, function(err, client) {
 ("Connected successfully to server");
 const db = (dbName);
 findData(db, function (result) {
  (result);
  ();
 })
});

Revise

// 
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
// Revisevar updateData = function (db, callback) {
 // Get the document collection var collection = ('collection3');
 var whereStr = {"name": "Li Ergou002"};
 var updateStr = {$set: {"age": 100}};
 // Modify the document (whereStr, updateStr, function (err, result) {
  if(err) {
   ('Error: ' + err);
   return;
  }
  callback(result);
 })
}
// Use connect method to connect to the server
(url, { useNewUrlParser: true }, function(err, client) {
 ("Connected successfully to server");
 const db = (dbName);
 updateData(db, function (result) {
  (result);
  ();
 })
});

delete

// 
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
// deletevar delData = function (db, callback) {
 // Get the document collection var collection = ('collection3');
 var whereStr = {"name": "Li Ergou002"};
 // Delete the document (whereStr, function (err, result) {
  if(err) {
   ('Error: ' + err);
   return;
  }
  callback(result);
 })
}
// Use connect method to connect to the server
(url, { useNewUrlParser: true }, function(err, client) {
 ("Connected successfully to server");
 const db = (dbName);
  delData(db, function (result) {
  (result);
  ();
 })
});

refer to:

/package/mongodb
https:///article/
https:///article/

I hope this article will be helpful to everyone's programming.