Skip to content
homexxhh edited this page Jan 26, 2016 · 1 revision

用的是Monogodb native driver(官方例子):

//这个是用的是连接池的方式,不会重复创建连接,利用现成的连接
var express = require('express');
var mongodb = require('mongodb');
var app = express();

var MongoClient = require('mongodb').MongoClient;
var db;

// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, database) {
  if(err) throw err;

  db = database;

  // Start the application after the database connection is ready
  app.listen(3000);
  console.log("Listening on port 3000");
});

// Reuse database object in request handlers
app.get("/", function(req, res) {
  db.collection("replicaset_mongo_client_collection").find({}, function(err, docs) {
    docs.each(function(err, doc) {
      if(doc) {
        console.log(doc);
      }
      else {
        res.end();
      }
    });
  });
});

参考官方文档:https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html

Node.js MongoDB Driver API:http://mongodb.github.io/node-mongodb-native/2.1/api/index.html

MONGODB NODE.JS DRIVER 2.0:http://mongodb.github.io/node-mongodb-native/2.0/getting-started/quick-tour/

Clone this wiki locally