Skip to content

Commit

Permalink
Merge remote branch 'kchodorow/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Dec 29, 2010
2 parents 76caaee + a577c3c commit ec7493c
Show file tree
Hide file tree
Showing 6 changed files with 338 additions and 17 deletions.
5 changes: 4 additions & 1 deletion examples/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ test = require("assert");

var Db = require('../lib/mongodb').Db,
Connection = require('../lib/mongodb').Connection,
Server = require('../lib/mongodb').Server,
Server = require('../lib/mongodb').Server,
// BSON = require('../lib/mongodb').BSONPure;
BSON = require('../lib/mongodb').BSONNative;

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;



sys.puts("Connecting to " + host + ":" + port);

var db = new Db('node-mongo-examples', new Server(host, port, {}), {native_parser:true});
db.open(function(err, db) {
db.dropDatabase(function() {
Expand Down
144 changes: 144 additions & 0 deletions examples/replSetServersQueries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
GLOBAL.DEBUG = true;

sys = require("sys");
test = require("assert");

var Db = require('../lib/mongodb').Db,
Connection = require('../lib/mongodb').Connection,
Server = require('../lib/mongodb').Server,
ReplSetServers = require('../lib/mongodb').ReplSetServers,
// BSON = require('../lib/mongodb').BSONPure;
BSON = require('../lib/mongodb').BSONNative;

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 port1 = 27018;
var port2 = 27019;
var server = new Server(host, port, {});
var server1 = new Server(host, port1, {});
var server2 = new Server(host, port2, {});
var servers = new Array();
servers[0] = server2;
servers[1] = server1;
servers[2] = server;

var replStat = new ReplSetServers(servers);
sys.puts("Connecting to " + host + ":" + port);
sys.puts("Connecting to " + host1 + ":" + port1);
sys.puts("Connecting to " + host2 + ":" + port2);
var db = new Db('node-mongo-examples', replStat, {native_parser:true});
db.open(function(err, db) {
db.dropDatabase(function() {
// Fetch the collection test
db.collection('test', function(err, collection) {
// Remove all records in collection if any
collection.remove(function(err, collection) {
// Insert three records
collection.insert([{'a':1}, {'a':2}, {'b':3}], function(docs) {
// Count the number of records
collection.count(function(err, count) {
sys.puts("There are " + count + " records.");
});

// Find all records. find() returns a cursor
collection.find(function(err, cursor) {
// Print each row, each document has an _id field added on insert
// to override the basic behaviour implement a primary key factory
// that provides a 12 byte value
sys.puts("Printing docs from Cursor Each")
cursor.each(function(err, doc) {
if(doc != null) sys.puts("Doc from Each " + sys.inspect(doc));
})
});
// Cursor has an to array method that reads in all the records to memory
collection.find(function(err, cursor) {
cursor.toArray(function(err, docs) {
sys.puts("Printing docs from Array")
docs.forEach(function(doc) {
sys.puts("Doc from Array " + sys.inspect(doc));
});
});
});

// Different methods to access records (no printing of the results)

// Locate specific document by key
collection.find({'a':1}, function(err, cursor) {
cursor.nextObject(function(err, doc) {
sys.puts("Returned #1 documents");
});
});

// Find records sort by 'a', skip 1, limit 2 records
// Sort can be a single name, array, associate array or ordered hash
collection.find({}, {'skip':1, 'limit':1, 'sort':'a'}, function(err, cursor) {
cursor.toArray(function(err, docs) {
sys.puts("Returned #" + docs.length + " documents");
})
});

// Find all records with 'a' > 1, you can also use $lt, $gte or $lte
collection.find({'a':{'$gt':1}}, function(err, cursor) {
cursor.toArray(function(err, docs) {
sys.puts("Returned #" + docs.length + " documents");
});
});

collection.find({'a':{'$gt':1, '$lte':3}}, function(err, cursor) {
cursor.toArray(function(err, docs) {
sys.puts("Returned #" + docs.length + " documents");
});
});

// Find all records with 'a' in a set of values
collection.find({'a':{'$in':[1,2]}}, function(err, cursor) {
cursor.toArray(function(err, docs) {
sys.puts("Returned #" + docs.length + " documents");
});
});

// Find by regexp
collection.find({'a':/[1|2]/}, function(err, cursor) {
cursor.toArray(function(err, docs) {
sys.puts("Returned #" + docs.length + " documents");
});
});

// Print Query explanation
collection.find({'a':/[1|2]/}, function(err, cursor) {
cursor.explain(function(err, doc) {
sys.puts("-------------------------- Explanation");
sys.puts(sys.inspect(doc));
})
});

// Use a hint with a query, hint's can also be store in the collection
// and will be applied to each query done through the collection.
// Hint's can also be specified by query which will override the
// hint's associated with the collection
collection.createIndex('a', function(err, indexName) {
collection.hint = 'a';

// You will see a different explanation now that the hint was set
collection.find({'a':/[1|2]/}, function(err, cursor) {
cursor.explain(function(err, doc) {
sys.puts("-------------------------- Explanation");
sys.puts(sys.inspect(doc));
})
});

collection.find({'a':/[1|2]/}, {'hint':'a'}, function(err, cursor) {
cursor.explain(function(err, doc) {
sys.puts("-------------------------- Explanation");
sys.puts(sys.inspect(doc));
db.close();
})
});
});
});
});
});
});
});
76 changes: 76 additions & 0 deletions examples/replSetServersSimple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
GLOBAL.DEBUG = true;

sys = require("sys");
test = require("assert");

var Db = require('../lib/mongodb').Db,
Admin = require('../lib/mongodb').Admin,
DbCommand = require('../lib/mongodb/commands/db_command').DbCommand,
Connection = require('../lib/mongodb').Connection,
Server = require('../lib/mongodb').Server,
ServerCluster = require('../lib/mongodb').ServerCluster,
// BSON = require('../lib/mongodb').BSONPure;
ReplSetServers = require('../lib/mongodb').ReplSetServers,
CheckMaster = require('../lib/mongodb').CheckMaster,
BSON = require('../lib/mongodb').BSONNative;

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 port1 = 27018;
var port2 = 27019;


sys.puts("Connecting to " + host + ":" + port);
sys.puts("Connecting to " + host + ":" + port1);
sys.puts("Connecting to " + host + ":" + port2);

var server = new Server(host, port, {});
var server1 = new Server(host, port1, {});
var server2 = new Server(host, port2, {});
var servers = new Array();
servers[0] = server2;
servers[1] = server1;
servers[2] = server;

var replStat = new ReplSetServers(servers);


var db = new Db('mongo-example', replStat, {native_parser:true});

db.open(function(err, db) {

db.dropDatabase(function(err, result) {
db.collection('test', function(err, collection) {
collection.remove(function(err, collection) {
// Insert 3 records
for(var i = 0; i < 3; i++) {
collection.insert({'a':i});
}

collection.count(function(err, count) {
sys.puts("There are " + count + " records in the test collection. Here they are:");

collection.find(function(err, cursor) {
cursor.each(function(err, item) {
if(item != null) {
sys.puts(sys.inspect(item));
sys.puts("created at " + new Date(item._id.generationTime) + "\n")
}
// Null signifies end of iterator
if(item == null) {
// Destory the collection
collection.drop(function(err, collection) {
db.close();
});
}
});
});
});
});
});
});
});



6 changes: 3 additions & 3 deletions lib/mongodb/admin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var Collection = require('./collection').Collection,
Cursor = require('./cursor').Cursor,
DbCommand = require('./commands/db_command').DbCommand;
Cursor = require('./cursor').Cursor,
DbCommand = require('./commands/db_command').DbCommand;

var Admin = exports.Admin = function(db) {
this.db = db;
Expand Down Expand Up @@ -75,4 +75,4 @@ Admin.prototype.validatCollection = function(collectionName, callback) {
callback(null, doc);
}
});
};
};
42 changes: 39 additions & 3 deletions lib/mongodb/connection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var net = require('net'),
Db = require('./Db').Db,
EventEmitter = require("events").EventEmitter,
BinaryParser = require('./bson/binary_parser').BinaryParser,
inherits = require('sys').inherits;
Expand Down Expand Up @@ -102,7 +103,8 @@ Connection.prototype.send = function(command) {
var self = this;
// Check if the connection is closed
try {
this.connection.write(command.toBinary(), "binary");

this.connection.write(command.toBinary(), "binary");
} catch(err) {
// Check if the connection is closed
if(this.connection.readyState != "open" && this.autoReconnect) {
Expand Down Expand Up @@ -131,11 +133,31 @@ Connection.prototype.send = function(command) {
});
}
} else {
throw err;

throw err;

}
}
};
/**
* Wrtie command without an attempt of reconnect
* @param command
*/

Connection.prototype.sendwithoutReconnect = function(command) {
var self = this;
// Check if the connection is closed

try {
this.connection.write(command.toBinary(), "binary");
}

catch(err) {
// no need to reconnect since called by latest master
// and already went through send() function
throw err;
};
};
// Some basic defaults
Connection.DEFAULT_PORT = 27017;

Expand Down Expand Up @@ -230,4 +252,18 @@ var ServerCluster = exports.ServerCluster = function(servers) {

ServerCluster.prototype.setTarget = function(target) {
this.target = target;
};
};

/**
* ReplSetServers constructor provides master-slave functionality
*
* @param serverArr{Array of type Server}
* @return constructor of ServerCluster
*
*/
var ReplSetServers = exports.ReplSetServers = function(serverArr) {
return new ServerCluster(serverArr);
};



Loading

0 comments on commit ec7493c

Please sign in to comment.