加上这句 { useUnifiedTopology: true },
当前服务器发现和监视引擎已弃用,将在将来的版本中删除。要使用新的服务器发现和监视引擎,请将选项{useUnifiedTopology:true}传递给mongoclient构造函数
var MongoClient = require(‘mongodb’).MongoClient;
var url = “mongodb://localhost:27017/duan”;
MongoClient.connect(url,{ useUnifiedTopology: true }, function(err, db) {
if (err) throw err;
console.log(“数据库已创建!”);
db.close();
});
增删改查
增 createCollection, insertOne, insertMany
删 deleteOne, deleteMany
改 updateOne, updateMany
查 find,
1.增 创建一条数据createCollection 创建集合
insertOne
insertMany
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/runoob';
MongoClient.connect(url, 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();
});
});
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, 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();
});
});
2.删除 drop() 方法来删除集合
deleteOne, deleteMany
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("runoob");
var whereStr = { type: "en" }; // 查询条件
dbo.collection("site").deleteMany(whereStr, function(err, obj) {
if (err) throw err;
console.log(obj.result.n + " 条文档被删除");
db.close();
});
});
3.改 updateOne updateMany
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("runoob");
var whereStr = {"type":'en'}; // 查询条件
var updateStr = {$set: { "url" : "https://www.runoob.com" }};
dbo.collection("site").updateMany(whereStr, updateStr, function(err, res) {
if (err) throw err;
console.log(res.result.nModified + " 条文档被更新");
db.close();
});
});
4.查 find
dbo.collection(“site”). find({}).toArray(function(err, result) { // 返回集合中所有数据
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("runoob");
var whereStr = {"name":'菜鸟教程'}; // 查询条件
dbo.collection("site").find(whereStr).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
5.排序 (var mysort = { type: 1 }; // 按type字段,1升序 -1降序)
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("runoob");
var mysort = { type: 1 }; // 按type字段,1升序 -1降序
dbo.collection("site").find().sort(mysort).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
6.查询分页
limit() 方法,该方法只接受一个参数,指定了返回的条数。
skip() 方法,指定跳过的条数
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("runoob");
dbo.collection("site").find().skip(2).limit(2).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
7.promise 增删改查
const MongoClient = require("mongodb").MongoClient;
const url = "mongodb://localhost/";
MongoClient.connect(url).then((conn) => {
console.log("数据库已连接");
const test = conn.db("testdb").collection("test");
// 增加
test.insertOne({ "site": "runoob.com" }).then((res) => {
// 查询
return test.find().toArray().then((arr) => {
console.log(arr);
});
}).then(() => {
// 更改
return test.updateMany({ "site": "runoob.com" },
{ $set: { "site": "example.com" } });
}).then((res) => {
// 查询
return test.find().toArray().then((arr) => {
console.log(arr);
});
}).then(() => {
// 删除
return test.deleteMany({ "site": "example.com" });
}).then((res) => {
// 查询
return test.find().toArray().then((arr) => {
console.log(arr);
});
}).catch((err) => {
console.log("数据操作失败" + err.message);
}).finally(() => {
conn.close();
});
}).catch((err) => {
console.log("数据库连接失败");
});
本文介绍了如何使用Node.js的MongoDB驱动进行数据库的基本操作,包括创建集合、插入、查询、更新及删除文档等,并展示了如何实现排序、分页等功能。
1727

被折叠的 条评论
为什么被折叠?



