欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Node.js操作MongoDB数据库实例分析

程序员文章站 2022-06-15 15:50:18
本文实例讲述了node.js操作mongodb数据库。分享给大家供大家参考,具体如下: node.js操作mongodb npm init npm i mongodb -...

本文实例讲述了node.js操作mongodb数据库。分享给大家供大家参考,具体如下:

node.js操作mongodb

npm init
npm i mongodb --save

{
 "name": "test",
 "version": "1.0.0",
 "description": "",
 "main": "app.js",
 "scripts": {
  "test": "echo \"error: no test specified\" && exit 1"
 },
 "author": "",
 "license": "isc",
 "dependencies": {
  "mongodb": "^3.1.1"
 }
}

连接数据库

// connect.js
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
mongoclient.connect(url, { usenewurlparser: true }, function(err, client) {
 console.log("connected successfully to server");
 const db = client.db(dbname);
 client.close();
});

插入

// insert.js
const mongoclient = require('mongodb').mongoclient;
// connection url
const url = 'mongodb://localhost:27017';
// database name
const dbname = 'mydatabase';
// 插入
var insertdata = function (db, callback) {
 // 获取文档集合
 var collection = db.collection('collection3');
 var data = [{"name": "李二狗001", "age": 20}, {"name": "李二狗002", "age": 21}];
 // 插入文档
 collection.insert(data, function (err, result) {
  if(err) {
   console.log('error: ' + err);
   return;
  }
  callback(result);
 })
}
// use connect method to connect to the server
mongoclient.connect(url, { usenewurlparser: true }, function(err, client) {
 console.log("connected successfully to server");
 const db = client.db(dbname);
 insertdata(db, function (result) {
  console.log(result);
  client.close();
 });
});

查询

// find.js
const mongoclient = require('mongodb').mongoclient;
// connection url
const url = 'mongodb://localhost:27017';
// database name
const dbname = 'mydatabase';
// 查询
var finddata = function (db, callback) {
 // 获取文档集合
 var collection = db.collection('collection3');
 var wherestr = {"name": "李二狗001"};
 // 查询文档
 collection.find(wherestr).toarray(function (err, result) {
  if(err) {
   console.log('error: ' + err);
   return;
  }
  callback(result);
 })
}
// use connect method to connect to the server
mongoclient.connect(url, { usenewurlparser: true }, function(err, client) {
 console.log("connected successfully to server");
 const db = client.db(dbname);
 finddata(db, function (result) {
  console.log(result);
  client.close();
 })
});

修改

// update.js
const mongoclient = require('mongodb').mongoclient;
// connection url
const url = 'mongodb://localhost:27017';
// database name
const dbname = 'mydatabase';
// 修改
var updatedata = function (db, callback) {
 // 获取文档集合
 var collection = db.collection('collection3');
 var wherestr = {"name": "李二狗002"};
 var updatestr = {$set: {"age": 100}};
 // 修改文档
 collection.update(wherestr, updatestr, function (err, result) {
  if(err) {
   console.log('error: ' + err);
   return;
  }
  callback(result);
 })
}
// use connect method to connect to the server
mongoclient.connect(url, { usenewurlparser: true }, function(err, client) {
 console.log("connected successfully to server");
 const db = client.db(dbname);
 updatedata(db, function (result) {
  console.log(result);
  client.close();
 })
});

删除

// delete.js
const mongoclient = require('mongodb').mongoclient;
// connection url
const url = 'mongodb://localhost:27017';
// database name
const dbname = 'mydatabase';
// 删除
var deldata = function (db, callback) {
 // 获取文档集合
 var collection = db.collection('collection3');
 var wherestr = {"name": "李二狗002"};
 // 删除文档
 collection.remove(wherestr, function (err, result) {
  if(err) {
   console.log('error: ' + err);
   return;
  }
  callback(result);
 })
}
// use connect method to connect to the server
mongoclient.connect(url, { usenewurlparser: true }, function(err, client) {
 console.log("connected successfully to server");
 const db = client.db(dbname);
  deldata(db, function (result) {
  console.log(result);
  client.close();
 })
});

参考:



希望本文所述对大家node.js程序设计有所帮助。