很是详细的文档http://mongodb.github.io/node-mongodb-native/2.2/quick-start/quick-start/node
安装express 和 mongodb .git
npm install express mongodb --save
经过 MongoClient.connect(url, function(err, db) {})
API 链接github
'use strict'; const express = require("express"), mongoClient = require("mongodb").MongoClient; var app = express(), url = 'mongodb://localhost:27017/test'; app.listen(3000, function(err) { if (err) { console.log("has error"); } }); app.get("/", function(req, res) { mongoClient.connect(url, function(err, db) { if (err) { console.log("数据库链接失败") } res.send("链接成功"); db.close(); }) })
这样就链接成功了 .mongodb
用ES6 仍是更棒的, 不过以为配babel 比较麻烦.., 等到结尾的dao 层封装我会使用ES6的语法来完成数据库
提供了两个api,分别为 db.collection("student").insertOne()
& db.collection("student").insertMany
express
app.get("/", function(req, res) { mongoClient.connect(url, function(err, db) { if (err) { console.log("数据库链接失败") } db.collection("student").insertOne({ "name": "筱原明里", "age": "18" }, function(err, result) { if (err) { console.log(err); } res.send(result); }) db.collection("student").insertMany([{ "name": "远野贵树", "age": "18" }, { "name": "澄田花苗" }], function(err, result) { if (err) { console.log(err); } res.send(result); }) db.close(); }) })
经过db.collection().find() 会返回一个游标,经过游标的迭代来访问全部数据.npm
注意,each 迭代的过程是异步的 !json
app.get("/", function(req, res) { mongoClient.connect(url, function(err, db) { if (err) { console.log("数据库链接失败") } var collection = db.collection('student'), cursor = collection.find({}), result = []; cursor.each(function(err, doc) { console.log(doc) if (err) { console.log(err); } if (doc == null) { res.send(result); }else{ result.push(doc); } }); db.close(); }) })
可是经过each判断是否迭代完成并非很好的方式,mongo给这个游标赋予一个更好的方法
toArray
api
app.get("/", function(req, res) { mongoClient.connect(url, function(err, db) { if (err) { console.log("数据库链接失败") } var collection = db.collection('student'), cursor = collection.find({}); cursor.toArray(function(err, docs) { // docs 就是全部的文档 console.log(docs); }) db.close(); }) })
这样作是取出所有的数据,下面是分页查询呢
mongoDB 的分页查询很是方便,封装的skip
,limit
有点像 .net 中的EF
中的skip
,take
等方法.babel
app.get("/", function(req, res) { mongoClient.connect(url, function(err, db) { if (err) { console.log("数据库链接失败") } var collection = db.collection('student'), // 跳过5条再取5条 cursor = collection.find({}).skip(10).limit(5); cursor.toArray(function(err, docs) { // docs 就是全部的文档 console.log(docs); }) db.close(); }) })
实际固然不能这么写,稍后会封装一个DAO,在里面会使用参数进行分页
app.get("/", function(req, res) { mongoClient.connect(url, function(err, db) { if (err) { console.log("数据库链接失败") } db.collection('student').updateOne({ name: "远野贵树" }, { $set: { age: 20, gender: "男" } }, function(err, result) { if (err) { console.log(err); } else { console.log(result); } }) db.collection('student').updateMany({ name: "澄田花苗" }, { $set: { age: 20, gender: "女" } }, function(err, result) { if (err) { console.log(err); } else { res.send(result); } }) db.close(); }) })
删除一样包含两个api ,deleteMany
& deleteOne
.
app.get("/", function(req, res) { mongoClient.connect(url, function(err, db) { if (err) { console.log("数据库链接失败") } db.collection("student").deleteOne({ 'name': '澄田花苗' }, function(err, result) { res.send(result); }) db.collection("student").deleteMany({ 'name': '澄田花苗' }, function(err, result) { res.send(result); }) db.close(); }) })
每次像上面同样调用确定是不可行的,因此须要封装一个DAO层.
mongodbdao.js
/* * @Author: Administrator * @Date: 2017-03-13 17:14:40 * @Last Modified by: Administrator * @Last Modified time: 2017-03-13 20:24:23 */ 'use strict'; const mongoClient = require("mongodb").MongoClient, dburl = require("config").dburl; // 链接数据库,内部函数 function _connectDB(callback) { mongoClient.connect(dburl, function(err, db) { if (err) { console.log(err); return; } callback(err, db); } }) } exports.find = function(collectionName, json, pageOption, callback) { // 第 0 页,就跳过 0 条,第 1 页,跳过10条 ,取 10条 // skip & limit ,若是参数为0,那么就忽略参数 var skipNumber = pageOption.page * pageOption.count || 0, takeNumber = pageOption || 0, sort = pageOption.sort || {}; _connectDB(function(err, db) { db.collection(collectionName).find(json).skip(skipNumber).limit(takeNumber).sort(sort) toArray(function(err, docs) { callback(err, docs); db.close(); }); }) }; exports.insertOne = function(collectionName, json, callback) { _connectDB(function(err, db) { db.insertOne(collectionName).insertOne(function(err, res) { callback(err, res); db.close(); }) }) } exports.insertMany = function(collectionName, json, callback) { _connectDB(function(err, db) { db.insertOne(collectionName).insertMany(function(err, res) { callback(err, res); db.close(); }) }) } exports.deteleOne = function(collectionName, json, callback) { _connectDB(function(err, db) { db.collection(collectionName).deteleOne(json, function(err, res) { callback(err, res); db.close(); }) }) }; exports.deteleMany = function(collectionName, json, callback) { _connectDB(function(err, db) { db.collection(collectionName).deteleMany(json, function(err, res) { callback(err, res); db.close(); }) }) }; exports.updateOne = function(collectionName, jsonQeury, jsonSet, callback) { _connectDB(function(err, db) { db.collection(collectionName).updateOne(jsonQeury, { $set: jsonSet }, function(err, res) { callback(err, res); db.close(); }) }) }; exports.updateMany = function(collectionName, jsonQeury, jsonSet, callback) { _connectDB(function(err, db) { db.collection(collectionName).updateMany(jsonQeury, { $set: jsonSet }, function(err, res) { callback(err, res); db.close(); }) }) }; exports.getAllCount = function(collectionName, json, callback) { _connectDB(function(err, db) { db.collection(collectionName).count(json, function(err, count) { callback(err, count); db.close(); }) }) };
简单地完成了一个DAO 的封装,可是在项目中, 也是不会这样用的
由于有一个更强大的东西 mongooose
,它就至关于 EF
之于 ADO.NET
.