如何在一个VPS上连接Node.js到一个MongoDB数据库?
(by Adil Mezghouti)
介绍
在这篇学习指南里,我们会介绍如何在一个VPS上连接Node.js到一个MongoDB数据库,并做一些基本的数据操作。
以下是需要的软件组件:
<!--[if !supportLists]-->· <!--[endif]-->Ubuntu 12.04 x32 VPS
<!--[if !supportLists]-->· <!--[endif]-->MongoDB v2.4.6
<!--[if !supportLists]-->· <!--[endif]-->Node.js v0.10.20
<!--[if !supportLists]-->· <!--[endif]-->The MongoDB Node.js 驱动器
MongoDB
“MongoDB是一个面向文档的开源数据库,具有性能高,可用性强并且易扩展的特点。”
如果你不熟悉MongoDB或者未安装,请先查看这份指南。
先来确定MongoDB进程在运行:
ps -ef | grep mongo
输出应该是下面这样的东西:
mongodb 1307 1 0 02:27 ? 00:00:01 /usr/bin/mongod --config /etc/mongodb.conf
如果没有运行,从MongoDB bin目录给出以下命令:
mongod
MongoDB有一个控制台客户端,给出下列命令来启动它:
mongo
你会看到这样的一个输出(不用理会警告):
MongoDB shell version: 2.4.4
connecting to: test
Server has startup warnings:
Mon Oct 7 20:40:35.209 [initandlisten]
Mon Oct 7 20:40:35.209 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
>
运行这条命令,列出现存的数据库:
show dbs
运行这条命令,显示选中的数据库:
db
运行一下命令,切换到“测试”数据库,以显示它包含的集合:
use test
show collections
这里有一个命令列表,可以在控制台客户端使用,你可以敲入“help”获得完整的命令列表:
show dbs #show database names
show collections #show collections in current database
show users # show users in current database
show profile # show most recent system.profile entries with time >= 1ms
show logs # show the accessible logger names
show log [name] # prints out the last segment of log in memory, 'global' is default
use <db_name> # set current database
db.foo.find() # list objects in collection foo
db.foo.find( { a : 1 } ) #list objects in foo where a == 1
it #result of the last line evaluated; use to further iterate
exit #quit the mongo shell
Node.js
“Node.js是一个建立在Chrome的JavaScript运行环境的平台,可以轻松地构建快速,可伸缩性高的网络应用程序。Node.js采用事件驱动,非阻塞I/O模型,这样使其既实现轻量级,又具备高性能,是构建运行在分布式设备的数据密集型实时应用程序的完美选择”。
如果你还没有安装,请先查看这篇教程的介绍说明。
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
先确认一下Node.js进程在运行:
node -v
作为命令输出,你应该看一下Node.js版本。
MongoDB Node.js驱动器
这个驱动器是MongoDB官方支持的Node.js驱动器,用纯JavaScript写出来,提供一个本地异步Node.js接口到MongoDB。
使用npm来安装驱动器:
npm install mongodb
连接到MongoDB,并执行数据操作
现在是时候来写可以允许你的Node.js应用程序连接到MongoDB的代码了,有3步操作:从数据库连接,写入,读取。
要执行你的代码,我们需要创建一个新文档,取名为:'app.js'.
建好文档后,用你的首选编辑器添加下列代码:
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
MongoClient.connect('mongodb://127.0.0.1:27017/test', function (err, db) {
if (err) {
throw err;
} else {
console.log("successfully connected to the database");
}
db.close();
});
输入以下命令,执行app.js文档:
node app.js
你应该在输出中看到这样的结果:成功连接到数据库(successfully connected to the database)。
现在添加一些语句,向名字为“test_insert”的一个新集合插入东西。
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
if(err) throw err;
var collection = db.collection('test_insert');
collection.insert({a:2}, function(err, docs) {
collection.count(function(err, count) {
console.log(format("count = %s", count));
db.close();
});
});
});
添加另一些代码块来验证数据已经输入到数据库。
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
if(err) throw err;
var collection = db.collection('test_insert');
collection.insert({a:2}, function(err, docs) {
collection.count(function(err, count) {
console.log(format("count = %s", count));
});
});
// Locate all the entries using find
collection.find().toArray(function(err, results) {
console.dir(results);
// Let's close the db
db.close();
});
});
恭喜啦!现在你可以在一个VPS上,用一个Node.js应用程序从MongoDB数据库连接,插入,读取数据。
资源
<!--[if !supportLists]-->· <!--[endif]-->http://www.nodejs.org/
<!--[if !supportLists]-->· <!--[endif]-->http://docs.mongodb.org/ecosystem/drivers/node-js/
<!--[if !supportLists]-->· <!--[endif]-->http://www.mongodb.org/
<!--[if !supportLists]-->· <!--[endif]-->https://npmjs.org/
相关推荐
基于node.js、vue、mongodb等技术构建的web系统,界面美观,功能齐全,适合用作毕业设计、课程设计作业等,项目均经过测试,可快速部署运行! 基于node.js、vue、mongodb等技术构建的web系统,界面美观,功能齐全,...
[奥莱理] Node.js 应用程序开发 (MongoDB 及 Backbone 实现) (英文版) [奥莱理] Building Node Applications with MongoDB and Backbone (E-Book) ☆ 出版信息:☆ [作者信息] Mike Wilson [出版机构] 奥莱理 ...
一个基于express,node.js, jade模板,MongoDB数据库的博客系统.zip
Node.js 是一种领先的服务器端编程环境,MongoDB是最流行的NoSQL数据库,而AngularJS 正迅速成为基于MVC的前端开发的领先框架。它们结合在一起使得能够完全用JavaScript 创建从服务器到客户端浏览器的高性能站点和...
基于node.js、vue、mongodb等技术构建的web系统,界面美观,功能齐全,适合用作毕业设计、课程设计作业等,项目均经过测试,可快速部署运行! 基于node.js、vue、mongodb等技术构建的web系统,界面美观,功能齐全,...
Node.js+MongoDB+AngularJS Web开发.zip 《Node.js + Mongodb + AngularJS web开发》--源代码(2015.09.04) 1.Node.js学习必备; 2.MongoDB学习必备; 3.Mongoose学习必备; 4.Express框架学习必备
基于node.js、vue、mongodb等技术构建的web系统,界面美观,功能齐全,适合用作毕业设计、课程设计作业等,项目均经过测试,可快速部署运行! 基于node.js、vue、mongodb等技术构建的web系统,界面美观,功能齐全,...
Backend system based on node.js + Mongodb. 基于 node.js + Mongodb
基于node封装的一个MongoDB操作类库 包括增删改查 分页查询 批量操作 采用单例模式大大提高性能 使用时需新建一个config.js配置数据库地址 数据库名称 const Config ={ url:'数据库地址', dbName:'数据库' } ...
在MVC架构中使用Node.js设计了一个支持MongoDB的CRUD API.zip
使用node.js作为后台语言,mongodb作为数据库搭建的一个博客系统.zip
Node.js MongoDB AngularJSWeb开发.part2
基于node.js、vue、mongodb等技术构建的web系统,界面美观,功能齐全,适合用作毕业设计、课程设计作业等,项目均经过测试,可快速部署运行! 基于node.js、vue、mongodb等技术构建的web系统,界面美观,功能齐全,...
A simple tool for fund using Node.js and MongoDB | 基于Node.js和MongoDB实现的基金小工具
基于node.js与vue.js的校园考试系统项目源码.zip 介绍 使用Vue.js和node.js...安装完数据库,打开cmd命令行窗口 输入mongod启动mongodb服务器在打开一个cmd窗口,输入mongo连接mongodb(不要关闭命令行窗口) ......
使用node.js+express+bootstrap+mongodb做简易的博客系统.zip