Node.js操作MongoDB数据库实例分析
前端  /  管理员 发布于 2年前   135
本文实例讲述了Node.js操作MongoDB数据库。分享给大家供大家参考,具体如下: Node.js操作MongoDB 连接数据库 插入 查询 修改 删除 参考: https://www.npmjs.com/package/mongodb 希望本文所述对大家node.js程序设计有所帮助。npm initnpm 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.jsconst MongoClient = require('mongodb').MongoClient;// Connection URLconst url = 'mongodb://localhost:27017';// Database Nameconst dbName = 'mydatabase';// Use connect method to connect to the serverMongoClient.connect(url, { useNewUrlParser: true }, function(err, client) { console.log("Connected successfully to server"); const db = client.db(dbName); client.close();});
// insert.jsconst MongoClient = require('mongodb').MongoClient;// Connection URLconst url = 'mongodb://localhost:27017';// Database Nameconst 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 serverMongoClient.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.jsconst MongoClient = require('mongodb').MongoClient;// Connection URLconst url = 'mongodb://localhost:27017';// Database Nameconst 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 serverMongoClient.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.jsconst MongoClient = require('mongodb').MongoClient;// Connection URLconst url = 'mongodb://localhost:27017';// Database Nameconst 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 serverMongoClient.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.jsconst MongoClient = require('mongodb').MongoClient;// Connection URLconst url = 'mongodb://localhost:27017';// Database Nameconst 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 serverMongoClient.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(); })});
/article/58815.htm
/article/98813.htm您可能感兴趣的文章:
博主 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..xiaoB 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..BUG4 在
你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..博主 在
go语言+beego框架中获取get,post请求的所有参数中评论 @ t1 直接在router.go文件中配就ok..Jade 在
如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号