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();
});
});

上一篇:springBoot入门教程(图文+源码+sql)


下一篇:python and mongodb---学习笔记