node.js使用mongodb
2014-01-11 00:35
483 查看
步骤:
1)先创建mongotest工程
D:\>cd D:\nodejs
D:\nodejs>express mongotest
create : mongotest
create : mongotest/package.json
create : mongotest/app.js
create : mongotest/public
create : mongotest/public/images
create : mongotest/public/stylesheets
create : mongotest/public/stylesheets/style.css
create : mongotest/routes
create : mongotest/routes/index.js
create : mongotest/routes/user.js
create : mongotest/public/javascripts
create : mongotest/views
create : mongotest/views/layout.jade
create : mongotest/views/index.jade
install dependencies:
$ cd mongotest && npm install
run the app:
$ node app
2)然后再package.json文件中添加依赖。
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.4.7",
"jade": "*",
"mongoose":">=2.3.1"
}
}
3)安装依赖:
D:\nodejs>npm install
npm WARN package.json example02@0.0.1 No description
npm WARN package.json example02@0.0.1 No repository field.
npm WARN package.json example02@0.0.1 No README data
npm WARN prefer global npm@1.3.21 should be installed with -g
D:\nodejs>cd mongotest
D:\nodejs\mongotest>npm install
npm http GET https://registry.npmjs.org/jade
npm http GET https://registry.npmjs.org/express/3.4.7
npm http GET https://registry.npmjs.org/mongoose
npm http 200 https://registry.npmjs.org/express/3.4.7
npm http GET https://registry.npmjs.org/express/-/express-3.4.7.tgz
4)安装完之后,可以在应用程序中请求并且使用。
var mongoose=require('mongoose');
mongoose.connect('mongodb://localhost/you_database',function(err){
if(!err){
console.log("connected to mongodb");
}else{
throw err;
}
});
5)测试:
使用node app.js,然后得出如下结果:
D:\nodejs\mongotest>node app.js
Express server listening on port 3000
D:\nodejs\mongotest\app.js:17
throw err;
^
Error: failed to connect to [localhost:27017]
at null.<anonymous> (D:\nodejs\mongotest\node_modules\mongoose\node_modules\mongodb
\lib\mongodb\connection\server.js:553:74)
at EventEmitter.emit (events.js:106:17)
at null.<anonymous> (D:\nodejs\mongotest\node_modules\mongoose\node_modules\mongodb
\lib\mongodb\connection\connection_pool.js:140:15)
at EventEmitter.emit (events.js:98:17)
at Socket.<anonymous> (D:\nodejs\mongotest\node_modules\mongoose\node_modules
\mongodb\lib\mongodb\connection\connection.js:512:10)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:441:14
at process._tickCallback (node.js:415:13)
D:\nodejs\mongotest>
这是因为没有启动mongodb的原因,所以启动本地mongodb,然后在测试。
D:\nodejs\mongotest>node app.js
Express server listening on port 3000
connected to mongodb
然后到mongodb中看看是否有库存在。
D:\>cd D:\mongodb\mongodb-win32-i386-2.4.8\bin
D:\mongodb\mongodb-win32-i386-2.4.8\bin>mongo
MongoDB shell version: 2.4.8
connecting to: test
Server has startup warnings:
Sat Jan 11 00:30:22.319 [initandlisten]
Sat Jan 11 00:30:22.319 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Sat Jan 11 00:30:22.319 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Sat Jan 11 00:30:22.334 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Sat Jan 11 00:30:22.334 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Sat Jan 11 00:30:22.334 [initandlisten]
> show dbs
local 0.03125GB
test 0.0625GB
> use local
switched to db local
> show collections
startup_log
> use test
switched to db test
> show collections
person
system.indexes
>
哈哈,还没有数据,可能是因为没有插入数据的原因吧。
到现在为止,node.js链接mongodb成功了。
1)先创建mongotest工程
D:\>cd D:\nodejs
D:\nodejs>express mongotest
create : mongotest
create : mongotest/package.json
create : mongotest/app.js
create : mongotest/public
create : mongotest/public/images
create : mongotest/public/stylesheets
create : mongotest/public/stylesheets/style.css
create : mongotest/routes
create : mongotest/routes/index.js
create : mongotest/routes/user.js
create : mongotest/public/javascripts
create : mongotest/views
create : mongotest/views/layout.jade
create : mongotest/views/index.jade
install dependencies:
$ cd mongotest && npm install
run the app:
$ node app
2)然后再package.json文件中添加依赖。
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.4.7",
"jade": "*",
"mongoose":">=2.3.1"
}
}
3)安装依赖:
D:\nodejs>npm install
npm WARN package.json example02@0.0.1 No description
npm WARN package.json example02@0.0.1 No repository field.
npm WARN package.json example02@0.0.1 No README data
npm WARN prefer global npm@1.3.21 should be installed with -g
D:\nodejs>cd mongotest
D:\nodejs\mongotest>npm install
npm http GET https://registry.npmjs.org/jade
npm http GET https://registry.npmjs.org/express/3.4.7
npm http GET https://registry.npmjs.org/mongoose
npm http 200 https://registry.npmjs.org/express/3.4.7
npm http GET https://registry.npmjs.org/express/-/express-3.4.7.tgz
4)安装完之后,可以在应用程序中请求并且使用。
var mongoose=require('mongoose');
mongoose.connect('mongodb://localhost/you_database',function(err){
if(!err){
console.log("connected to mongodb");
}else{
throw err;
}
});
5)测试:
使用node app.js,然后得出如下结果:
D:\nodejs\mongotest>node app.js
Express server listening on port 3000
D:\nodejs\mongotest\app.js:17
throw err;
^
Error: failed to connect to [localhost:27017]
at null.<anonymous> (D:\nodejs\mongotest\node_modules\mongoose\node_modules\mongodb
\lib\mongodb\connection\server.js:553:74)
at EventEmitter.emit (events.js:106:17)
at null.<anonymous> (D:\nodejs\mongotest\node_modules\mongoose\node_modules\mongodb
\lib\mongodb\connection\connection_pool.js:140:15)
at EventEmitter.emit (events.js:98:17)
at Socket.<anonymous> (D:\nodejs\mongotest\node_modules\mongoose\node_modules
\mongodb\lib\mongodb\connection\connection.js:512:10)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:441:14
at process._tickCallback (node.js:415:13)
D:\nodejs\mongotest>
这是因为没有启动mongodb的原因,所以启动本地mongodb,然后在测试。
D:\nodejs\mongotest>node app.js
Express server listening on port 3000
connected to mongodb
然后到mongodb中看看是否有库存在。
D:\>cd D:\mongodb\mongodb-win32-i386-2.4.8\bin
D:\mongodb\mongodb-win32-i386-2.4.8\bin>mongo
MongoDB shell version: 2.4.8
connecting to: test
Server has startup warnings:
Sat Jan 11 00:30:22.319 [initandlisten]
Sat Jan 11 00:30:22.319 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Sat Jan 11 00:30:22.319 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Sat Jan 11 00:30:22.334 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Sat Jan 11 00:30:22.334 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Sat Jan 11 00:30:22.334 [initandlisten]
> show dbs
local 0.03125GB
test 0.0625GB
> use local
switched to db local
> show collections
startup_log
> use test
switched to db test
> show collections
person
system.indexes
>
哈哈,还没有数据,可能是因为没有插入数据的原因吧。
到现在为止,node.js链接mongodb成功了。
相关文章推荐
- 使用React、Node.js、MongoDB、Socket.IO开发一个角色投票应用的学习过程(一)
- 使用Node.js + MongoDB 构建restful API
- 使用React、Node.js、MongoDB、Socket.IO开发一个角色投票应用的学习过程(二)
- 使用Node.js + MongoDB实现一个简单的日志分析系统
- 使用Nodejs+mongodb开发地图瓦片服务器
- Nodejs + mongoDB 使用初体验
- 使用node.js ,Express, 和Mongodb 简历一个简单的Restful 网页服务-part1
- 使用node.js的第三方模块Mongoose访问mongodb(六)
- 详解Wondows下Node.js使用MongoDB的环境配置
- 使用Node.js + Mongodb构建基于Cloud Foundry的项目
- Nodejs + mongoDB 使用初体验
- 编写一个使用 Node.js/MongoDB Web 服务的 iOS 应用
- Node.js使用MongoDB3.4+Access control is not enabled for the database解决方案
- node.js学习笔记(4)_极客学院_MongoDB的使用
- Node.js Mongodb非关系型数据库,安装 配置 使用
- mongodb副本集的配置及node.js的使用
- Node.js使用MongoDB
- nodejs实战《一起学 Node.js》 使用 Express + MongoDB 搭建多人博客
- 使用React、Node.js、MongoDB、Socket.IO开发一个角色投票应用的学习过程(三)
- node.js使用mongodb存储数据-概念篇+服务端使用