您的位置:首页 > 数据库 > Mongodb

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成功了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  node.js