免責聲明

Disclaimer (免責聲明)
繼續閱覽代表您接受以上的免責聲明.
To continue reading means you accept the above disclaimer.

2015年6月15日 星期一

mongodb adaptor for nodejs


//=== http://stackoverflow.com/questions/24736999/how-can-i-require-mongodb-within-casperjs

install mongodb driver for nodejs
$ npm install mongodb

* this mongodb module is just an adaptor for nodejs to access the actual mongodb server,
you have to setup the mongodb server beside the module.



//=== https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html

MongoClient class's instance member functions:
connect(), open(), close(), db()

connect(url[, options], callback)
open(callback)
close(callback)
db(dbName)



"""...
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON,
assert = require('assert');

// Set up the connection to the local db
var mongoclient = new MongoClient(new Server("localhost", 27017), {native_parser: true});

// Open the connection to the server
mongoclient.open(function(err, mongoclient) {

// Get the first db and do an update document on it
var db = mongoclient.db("integration_tests");
db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
assert.equal(null, err);
assert.equal(1, result);

// Get another db and do an update document on it
var db2 = mongoclient.db("integration_tests2");
db2.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
assert.equal(null, err);
assert.equal(1, result);

// Close the connection
mongoclient.close();
});
});
});

..."""


//=== static member function, MongoClient.connect(url, cb)
Connect to MongoDB using a url as documented


var MongoClient = require('mongodb').MongoClient;
var assert = require('assert')
var ObjectId = require('mongodb').ObjectID;
var url = 'mongodb://myMongo1:27017/test';


db.collection('restaurants1').insertOne();

MongoClient.connect(url, function(err, db){});







沒有留言:

張貼留言