摘要:本篇Node.js教程探讨了如何使用Node.js操作MongoDB,希望阅读本篇文章以后大家有所收获,帮助大家对Node.js的理解更加深入。
使用Node.js进行常用的数据库操作
· 连接数据库
1. 新建一个文件夹,新建package.json
{ "name":"Learn_MongoDB", "description":"Some usefull operation", "version":"0.0.1", "private":true, "dependencies":{ "mongodb":"3.0.7" } }
执行npm install
1. 连接数据库
const MongoClient = require("mongodb").MongoClient;const assert = require('assert');const url = 'mongodb://localhost:27017';const dbName = 'myProject'; MongoClient.connect(url,function(error,client){ assert.equal(null,error); console.log("Connected successfully to server"); const db = client.db(dbName); client.close(); }); · 插入数据 const MongoClient = require("mongodb").MongoClient;const assert = require('assert');const url = 'mongodb://localhost:27017';const dbName = 'myProject'; MongoClient.connect(url,function(error,client){ assert.equal(null,error); console.log("Connected successfully to server"); const db = client.db(dbName); insertDocuments(db,function(result){ client.close(); }); }); const insertDocuments = function(db,callback){ const collection = db.collection("documents"); collection.insertMany([{a:1},{a:2},{a:3}],function(error,result){ assert.equal(error,null); assert.equal(3,result.result.n); assert.equal(3,result.ops.length); console.log("inserted 3 documents into the collection"); callback(result); }); }
其中,插入成功后的result数据为
{ result: { ok: 1, n: 3 }, ops: [ { a: 1, _id: 5ae2cae2bc8929096f754712 }, { a: 2, _id: 5ae2cae2bc8929096f754713 }, { a: 3, _id: 5ae2cae2bc8929096f754714 } ], insertedCount: 3, insertedIds: { '0': 5ae2cae2bc8929096f754712, '1': 5ae2cae2bc8929096f754713, '2': 5ae2cae2bc8929096f754714 } } inserted 3 documents into the collection
· 查询数据
const MongoClient = require("mongodb").MongoClient;const assert = require('assert');const url = 'mongodb://localhost:27017';const dbName = 'myProject'; MongoClient.connect(url,function(error,client){ assert.equal(null,error); console.log("Connected successfully to server"); const db = client.db(dbName); findDocuments(db,function(docs){ client.close(); }); });const findDocuments = function(db, callback) { const collection = db.collection('documents'); collection.find({}).toArray(function(err, docs) { assert.equal(err, null); console.log("Found the following records"); console.log(docs) callback(docs); }); }
其中find({})表示查询所有的数据,
find({a:3})条件查询
· 更新数据
const MongoClient = require("mongodb").MongoClient;const assert = require('assert');const url = 'mongodb://localhost:27017';const dbName = 'myProject'; MongoClient.connect(url,function(error,client){ assert.equal(null,error); console.log("Connected successfully to server"); const db = client.db(dbName); updateDocument(db,function(result){ client.close(); }); });const updateDocument = function(db, callback) { const collection = db.collection('documents'); collection.updateOne({ a : 2 } , { $set: { b : 1 } }, function(err, result) { assert.equal(err, null); assert.equal(1, result.result.n); console.log("Updated the document with the field a equal to 2"); callback(result); }); }
· 删除数据
const MongoClient = require("mongodb").MongoClient;const assert = require('assert');const url = 'mongodb://localhost:27017';const dbName = 'myProject'; MongoClient.connect(url,function(error,client){ assert.equal(null,error); console.log("Connected successfully to server"); const db = client.db(dbName); removeDocument(db,function(result){ client.close(); }); });const removeDocument = function(db, callback) { const collection = db.collection('documents'); collection.deleteOne({ a : 3 }, function(err, result) { assert.equal(err, null); assert.equal(1, result.result.n); console.log("Removed the document with the field a equal to 3"); callback(result); }); }
· 创建索引
const indexCollection = function(db, callback) { db.collection('documents').createIndex( { "a": 1 }, null, function(err, results) { console.log(results); callback(); } ); };
本文由职坐标整理发布,更多相关知识,请关注职坐标WEB前端Node.js频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号