Node.js 连接 MongoDB数据库

Posted lishixiang-007

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Node.js 连接 MongoDB数据库相关的知识,希望对你有一定的参考价值。

安装指令:npm install mongodb

var mongodb = require("mongodb");
// console.log(mongodb);

var MongoClient = mongodb.MongoClient;

var CONN_DB_STR = "mongodb://localhost:27017/wh1807";

MongoClient.connect(CONN_DB_STR,(err,db)=>
if(err) throw err;
console.log("数据库链接成功...");
console.log(db);

var movie = db.collection("movie");
movie.find(,title:1,year:1,genres:1,_id:0).toArray((err,result)=>
if(err) throw err;
console.log("查询成功++++++++++++++++++++++++++++");
console.log(result);
);

// var collections = db.getCollectionNames(); error
// console.log(collections);

db.collection("user").insert(username:"helloNode",password:"daydayup",(err,result)=>
if(err) throw err;
console.log("insert ++++++++++++++++++++++++++++");
console.log(result);
)

db.collection("user").updateMany(
username:"helloNode",

$set:age:40,password:"666666"
,
(err,result)=>
if(err) throw err;
console.log("update ++++++++++++++++++++++++++++");
console.log(result);

)
db.collection(‘user‘).deleteOne(username:"helloNode",(err,result)=>
if(err) throw err;
console.log("deleteOne ++++++++++++++++++++++++++++");
console.log(result);
);
db.close(); // 关闭数据库
)

补充菜鸟教程:
(1)创建连接
var MongoClient = require(‘mongodb‘).MongoClient;
var url = "mongodb://localhost:27017/runoob";
MongoClient.connect(url, useNewUrlParser: true , function(err, db)
if (err) throw err;
console.log("数据库已创建!");
db.close();
);
(2)创建集合
var MongoClient = require(‘mongodb‘).MongoClient;
var url = ‘mongodb://localhost:27017/runoob‘;
MongoClient.connect(url, useNewUrlParser: true , function (err, db)
if (err) throw err;
console.log(‘数据库已创建‘);
var dbase = db.db("runoob");
dbase.createCollection(‘site‘, function (err, res)
if (err) throw err;
console.log("创建集合!");
db.close();
);
);
(3)插入一条数据
var MongoClient = require(‘mongodb‘).MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, useNewUrlParser: true , function(err, db)
if (err) throw err;
var dbo = db.db("runoob");
var myobj = name: "菜鸟教程", url: "www.runoob" ;
dbo.collection("site").insertOne(myobj, function(err, res)
if (err) throw err;
console.log("文档插入成功");
db.close();
);
);
(4)插入多条数据
var MongoClient = require(‘mongodb‘).MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, useNewUrlParser: true , function(err, db)
if (err) throw err;
var dbo = db.db("runoob");
var myobj = [
name: ‘菜鸟工具‘, url: ‘https://c.runoob.com‘, type: ‘cn‘,
name: ‘Google‘, url: ‘https://www.google.com‘, type: ‘en‘,
name: ‘Facebook‘, url: ‘https://www.google.com‘, type: ‘en‘
];
dbo.collection("site").insertMany(myobj, function(err, res)
if (err) throw err;
console.log("插入的文档数量为: " + res.insertedCount);
db.close();
);
);
(5)删除一条数据
var MongoClient = require(‘mongodb‘).MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, useNewUrlParser: true , function(err, db)
if (err) throw err;
var dbo = db.db("runoob");
var whereStr = "name":‘菜鸟教程‘; // 查询条件
dbo.collection("site").deleteOne(whereStr, function(err, obj)
if (err) throw err;
console.log("文档删除成功");
db.close();
);
);
(6)排序
var MongoClient = require(‘mongodb‘).MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, useNewUrlParser: true , function(err, db)
if (err) throw err;
var dbo = db.db("runoob");
var mysort = type: 1 ;
dbo.collection("site").find().sort(mysort).toArray(function(err, result)
if (err) throw err;
console.log(result);
db.close();
);
);
(7)删除集合
var MongoClient = require(‘mongodb‘).MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, useNewUrlParser: true , function(err, db)
if (err) throw err;
var dbo = db.db("runoob");
// 删除 test 集合
dbo.collection("test").drop(function(err, delOK) // 执行成功 delOK 返回 true,否则返回 false
if (err) throw err;
if (delOK) console.log("集合已删除");
db.close();
);
);

以上是关于Node.js 连接 MongoDB数据库的主要内容,如果未能解决你的问题,请参考以下文章

Node.JS中使用单例封装MongoDB

如何在 node.js 中重用 mongodb 连接

02_使用Node.js往mongodb插入数据

用Node.JS+MongoDB搭建个人博客(model目录)

Node.js基于Express框架 + 连接MongoDB + 写后端接口

在 Node JS 应用程序中实现单例 mongoDB DB 对象