Permalink
Browse files

Fixed examples

  • Loading branch information...
1 parent 6a983d4 commit 16addfc1ea1cd351e4f6cb279eda6d472a353323 @christkv christkv committed Jun 25, 2013
View
@@ -1,14 +1,11 @@
-var Db = require('../lib/mongodb').Db
- , Connection = require('../lib/mongodb').Connection
- , Server = require('../lib/mongodb').Server
+var MongoClient = require('../lib/mongodb').MongoClient
, format = require('util').format;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
-var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
-
+var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
console.log("Connecting to " + host + ":" + port);
-Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
+MongoClient.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
db.dropDatabase(function(err, result){
db.dropCollection('test', function(err, result) {
var collection = db.collection('test');
View
@@ -1,15 +1,13 @@
-var Db = require('../lib/mongodb').Db
- , Connection = require('../lib/mongodb').Connection
- , Server = require('../lib/mongodb').Server
+var MongoClient = require('../lib/mongodb').MongoClient
, format = require('util').format;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
-var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
+var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
var LINE_SIZE = 120;
console.log("Connecting to " + host + ":" + port);
-Db.connect(format("mongodb://%s:%s/node-mongo-blog?w=1", host, port), function(err, db) {
+MongoClient.connect(format("mongodb://%s:%s/node-mongo-blog?w=1", host, port), function(err, db) {
db.dropDatabase(function(err, result) {
console.log("===================================================================================");
console.log(">> Adding Authors");
View
@@ -1,13 +1,11 @@
-var Db = require('../lib/mongodb').Db
- , Connection = require('../lib/mongodb').Connection
- , Server = require('../lib/mongodb').Server
+var MongoClient = require('../lib/mongodb').MongoClient
, format = require('util').format;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
-var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
+var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
console.log("Connecting to " + host + ":" + port);
-Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
+MongoClient.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
db.dropCollection('test', function(err, result) {
// A capped collection has a max size and optionally a max number of records.
// Old records get pushed out by new ones once the size or max num records is
View
@@ -1,62 +1,59 @@
-var Db = require('../lib/mongodb').Db
- , Connection = require('../lib/mongodb').Connection
- , Server = require('../lib/mongodb').Server
+var MongoClient = require('../lib/mongodb').MongoClient
, format = require('util').format;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
-var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
+var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
console.log("Connecting to " + host + ":" + port);
-Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
- db.collection('test', function(err, collection) {
- // Erase all records from collection, if any
- collection.remove(function(err, result) {
-
- // Insert 3 records
- for(var i = 0; i < 3; i++) {
- collection.insert({'a':i}, {w:0});
- }
-
- // Cursors don't run their queries until you actually attempt to retrieve data
- // from them.
-
- // Find returns a Cursor, which is Enumerable. You can iterate:
- collection.find().each(function(err, item) {
- if(item != null) console.dir(item);
- });
-
- // You can turn it into an array
- collection.find().toArray(function(err, items) {
- console.log("count: " + items.length);
- });
-
- // You can iterate after turning it into an array (the cursor will iterate over
- // the copy of the array that it saves internally.)
- var cursor = collection.find();
- cursor.toArray(function(err, items) {
- cursor.each(function(err, item) {
- if(item != null) console.dir(item);
- });
- });
-
- // You can get the next object
- collection.find().nextObject(function(err, item) {
+MongoClient.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
+ var collection = db.collection('test');
+ // Erase all records from collection, if any
+ collection.remove(function(err, result) {
+
+ // Insert 3 records
+ for(var i = 0; i < 3; i++) {
+ collection.insert({'a':i}, {w:0});
+ }
+
+ // Cursors don't run their queries until you actually attempt to retrieve data
+ // from them.
+
+ // Find returns a Cursor, which is Enumerable. You can iterate:
+ collection.find().each(function(err, item) {
+ if(item != null) console.dir(item);
+ });
+
+ // You can turn it into an array
+ collection.find().toArray(function(err, items) {
+ console.log("count: " + items.length);
+ });
+
+ // You can iterate after turning it into an array (the cursor will iterate over
+ // the copy of the array that it saves internally.)
+ var cursor = collection.find();
+ cursor.toArray(function(err, items) {
+ cursor.each(function(err, item) {
if(item != null) console.dir(item);
});
-
- // next_object returns null if there are no more objects that match
- var cursor = collection.find();
+ });
+
+ // You can get the next object
+ collection.find().nextObject(function(err, item) {
+ if(item != null) console.dir(item);
+ });
+
+ // next_object returns null if there are no more objects that match
+ var cursor = collection.find();
+ cursor.nextObject(function(err, item) {
cursor.nextObject(function(err, item) {
cursor.nextObject(function(err, item) {
cursor.nextObject(function(err, item) {
- cursor.nextObject(function(err, item) {
- console.log("nextObject returned: ");
- console.dir(item);
- db.close();
- });
+ console.log("nextObject returned: ");
+ console.dir(item);
+ db.close();
});
- });
- });
+ });
+ });
});
});
});
View
@@ -1,14 +1,12 @@
-var Db = require('../lib/mongodb').Db
- , Connection = require('../lib/mongodb').Connection
- , Server = require('../lib/mongodb').Server
+var MongoClient = require('../lib/mongodb').MongoClient
, GridStore = require('../lib/mongodb').GridStore
, format = require('util').format;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
-var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
+var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
console.log(">> Connecting to " + host + ":" + port);
-Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
+MongoClient.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
// Write a new file
new GridStore(db, "foobar", "w").open(function(err, gridStore) {
gridStore.write("hello world!", function(err, gridStore) {
@@ -42,7 +40,7 @@ Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), functi
});
});
-Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), {native_parser:true}, function(err, db) {
+MongoClient.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), {native_parser:true}, function(err, db) {
// File existence tests
new GridStore(db, "foobar2", "w").open(function(err, gridStore) {
gridStore.write( 'hello sailor', function(err, gridStore) {
@@ -87,7 +85,7 @@ Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), {nativ
});
});
-Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), {native_parser:true}, function(err, db) {
+MongoClient.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), {native_parser:true}, function(err, db) {
// Metadata
new GridStore(db, "foobar3", "w").open(function(err, gridStore) {
gridStore.write('hello, world!', function(err, gridStore){});
View
@@ -1,14 +1,12 @@
-var Db = require('../lib/mongodb').Db
- , Connection = require('../lib/mongodb').Connection
- , Server = require('../lib/mongodb').Server
+var MongoClient = require('../lib/mongodb').MongoClient
, mongo = require('../lib/mongodb')
, format = require('util').format
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
-var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
+var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
console.log(">> Connecting to " + host + ":" + port);
-Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
+MongoClient.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
console.log(">> Dropping collection test");
db.dropCollection('test', function(err, result) {
console.log("dropped: ");
View
@@ -1,13 +1,11 @@
-var Db = require('../lib/mongodb').Db
- , Connection = require('../lib/mongodb').Connection
- , Server = require('../lib/mongodb').Server
+var MongoClient = require('../lib/mongodb').MongoClient
, format = require('util').format;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
-var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
+var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
console.log("Connecting to " + host + ":" + port);
-Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
+MongoClient.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
var collection = db.collection('test');
// Remove all existing documents in collection
View
@@ -1,11 +1,8 @@
-var Db = require('../lib/mongodb').Db
- , Connection = require('../lib/mongodb').Connection
- , Server = require('../lib/mongodb').Server
- , Cursor = require('../lib/mongodb').Cursor
+var MongoClient = require('../lib/mongodb').MongoClient
, format = require('util').format;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
-var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
+var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
Slave = function() {
this.running = false;
@@ -21,17 +18,18 @@ Slave.prototype.start = function() {
var self = this;
if (this.running) return;
- Db.connect(format("mongodb://%s:%s/testing?w=1", host, port), function(err, db) {
+ MongoClient.connect(format("mongodb://%s:%s/testing?w=1", host, port), function(err, db) {
if (err) {
console.log('> MongoSlave error' + err);
process.exit(1);
}
self.db = db;
- db.collection('local.oplog.$main', function(err, collection) {
+ db.collection('local.oplog.$main', {strict: true}, function(err, collection) {
if (! collection) {
console.log('> MongoSlave - local.oplog.$main not found');
+ db.close();
self.stop();
return false;
}
@@ -41,17 +39,15 @@ Slave.prototype.start = function() {
});
//get last row for init TS
- collection.find({}, {'limit': 1, 'sort': [['$natural', -1]]}, function(err, cursor) {
- cursor.toArray(function(err, items) {
- if (items.length) {
- console.log('> MongoSlave started');
- self.running = true;
- self._runSlave(collection, items[0]['ts']);
- } else if (err) {
- console.log(err);
- self.stop();
- }
- });
+ collection.find({}, {'limit': 1, 'sort': [['$natural', -1]]}).toArray(function(err, items) {
+ if (items.length) {
+ console.log('> MongoSlave started');
+ self.running = true;
+ self._runSlave(collection, items[0]['ts']);
+ } else if (err) {
+ console.log(err);
+ self.stop();
+ }
});
});
});
@@ -70,30 +66,26 @@ Slave.prototype._runSlave = function(collection, time) {
var self = this;
//watch oplog INFINITE (until Slave.stop())
- collection.find({'ts': {'$gt': time}}, {'tailable': 1, 'sort': [['$natural', 1]]}, function(err, cursor) {
- cursor.each(function(err, item) {
- if (cursor.state == Cursor.CLOSED) { //broken cursor
- self.running && self._runSlave(collection, time);
- return;
- }
- time = item['ts'];
-
- switch(item['op']) {
- case 'i': //inserted
- self._emitObj(item['o']);
- break;
- case 'u': //updated
- self.db.collection(item['ns'], function(err, collection) {
- collection.findOne(item['o2']['_id'], {}, function(err, item) {
- item && self._emitObj(item);
- });
- });
- break;
- case 'd': //deleted
- //nothing to do
- break;
- }
- });
+ collection.find({'ts': {'$gt': time}}, {'tailable': 1, 'sort': [['$natural', 1]]}).each(function(err, item) {
+ if (cursor.state == Cursor.CLOSED) { //broken cursor
+ self.running && self._runSlave(collection, time);
+ return;
+ }
+ time = item['ts'];
+
+ switch(item['op']) {
+ case 'i': //inserted
+ self._emitObj(item['o']);
+ break;
+ case 'u': //updated
+ self.db.collection(item['ns']).findOne(item['o2']['_id'], {}, function(err, item) {
+ item && self._emitObj(item);
+ });
+ break;
+ case 'd': //deleted
+ //nothing to do
+ break;
+ }
});
}
View
@@ -1,14 +1,12 @@
-var Db = require('../lib/mongodb').Db
- , Connection = require('../lib/mongodb').Connection
- , Server = require('../lib/mongodb').Server
+var MongoClient = require('../lib/mongodb').MongoClient
, format = require('util').format;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
-var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : Connection.DEFAULT_PORT;
+var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
console.log("Connecting to " + host + ":" + port);
-Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
+MongoClient.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
db.dropDatabase(function() {
// Fetch the collection test
var collection = db.collection('test');
Oops, something went wrong.

0 comments on commit 16addfc

Please sign in to comment.