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成功了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值