Permalink
Browse files

Cleanup of examples and small bug fix

  • Loading branch information...
1 parent 8b6bf50 commit 2cd47ab4fae29d4aa658e9cfeb79c672784ed68a @christkv christkv committed Nov 7, 2012
Showing with 237 additions and 338 deletions.
  1. +21 −29 examples/cursor.js
  2. +17 −28 examples/gridfs.js
  3. +42 −43 examples/index.js
  4. +33 −36 examples/info.js
  5. +8 −6 examples/oplog.js
  6. +85 −108 examples/queries.js
  7. +20 −21 examples/simple.js
  8. +0 −36 examples/strict.js
  9. +6 −6 examples/types.js
  10. +0 −12 examples/url.js
  11. +3 −11 lib/mongodb/collection.js
  12. +2 −2 lib/mongodb/db.js
View
@@ -14,57 +14,49 @@ Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), functi
// Insert 3 records
for(var i = 0; i < 3; i++) {
- collection.insert({'a':i}, {safe:{w:0}});
+ 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(function(err, cursor) {
- cursor.each(function(err, item) {
- if(item != null) console.dir(item);
- });
+ collection.find().each(function(err, item) {
+ if(item != null) console.dir(item);
});
// You can turn it into an array
- collection.find(function(err, cursor) {
- cursor.toArray(function(err, items) {
- console.log("count: " + items.length);
- });
+ 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.)
- collection.find(function(err, cursor) {
- cursor.toArray(function(err, items) {
- cursor.each(function(err, item) {
- if(item != null) console.dir(item);
- });
+ 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(function(err, cursor) {
- cursor.nextObject(function(err, item) {
- if(item != null) console.dir(item);
- });
+ collection.find().nextObject(function(err, item) {
+ if(item != null) console.dir(item);
});
- // next_object returns null if there are no more objects that match
- collection.find(function(err, cursor) {
+ // 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,33 +1,30 @@
-var Db = require('../lib/mongodb').Db,
- Connection = require('../lib/mongodb').Connection,
- Server = require('../lib/mongodb').Server,
- GridStore = require('../lib/mongodb').GridStore;
+var Db = require('../lib/mongodb').Db
+ , Connection = require('../lib/mongodb').Connection
+ , Server = require('../lib/mongodb').Server
+ , 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;
console.log(">> Connecting to " + host + ":" + port);
-var db1 = new Db('node-mongo-examples', new Server(host, port, {}), {native_parser:true});
-db1.open(function(err, db) {
+Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), function(err, db) {
// Write a new file
- var gridStore = new GridStore(db, "foobar", "w");
- gridStore.open(function(err, gridStore) {
+ new GridStore(db, "foobar", "w").open(function(err, gridStore) {
gridStore.write("hello world!", function(err, gridStore) {
gridStore.close(function(err, result) {
// Read the file and dump the contents
dump(db, 'foobar');
// Append more data
- gridStore = new GridStore(db, 'foobar', "w+");
- gridStore.open(function(err, gridStore) {
+ new GridStore(db, 'foobar', "w+").open(function(err, gridStore) {
gridStore.write('\n', function(err, gridStore) {
gridStore.puts('line two', function(err, gridStore) {
gridStore.close(function(err, result) {
dump(db, 'foobar');
// Overwrite
- gridStore = new GridStore(db, 'foobar', "w");
- gridStore.open(function(err, gridStore) {
+ new GridStore(db, 'foobar', "w").open(function(err, gridStore) {
gridStore.write('hello, sailor!', function(err, gridStore) {
gridStore.close(function(err, result) {
dump(db, 'foobar', function() {
@@ -45,11 +42,9 @@ db1.open(function(err, db) {
});
});
-var db2 = new Db('node-mongo-examples', new Server(host, port, {}), {native_parser:true});
-db2.open(function(err, db) {
+Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), {native_parser:true}, function(err, db) {
// File existence tests
- var gridStore = new GridStore(db, "foobar2", "w");
- gridStore.open(function(err, gridStore) {
+ new GridStore(db, "foobar2", "w").open(function(err, gridStore) {
gridStore.write( 'hello sailor', function(err, gridStore) {
gridStore.close(function(err, result) {
GridStore.exist(db, 'foobar2', function(err, result) {
@@ -66,8 +61,7 @@ db2.open(function(err, db) {
});
// Rewind/seek/tell
- var gridStore2 = new GridStore(db, 'foobar2', 'w');
- gridStore2.open(function(err, gridStore) {
+ new GridStore(db, 'foobar2', 'w').open(function(err, gridStore) {
gridStore.write('hello, world!', function(err, gridStore){});
gridStore.rewind(function(){});
gridStore.write('xyzzz', function(err, gridStore){});
@@ -93,11 +87,9 @@ db2.open(function(err, db) {
});
});
-var db3 = new Db('node-mongo-examples', new Server(host, port, {}), {native_parser:true});
-db3.open(function(err, db) {
+Db.connect(format("mongodb://%s:%s/node-mongo-examples?w=1", host, port), {native_parser:true}, function(err, db) {
// Metadata
- var gridStore = new GridStore(db, "foobar3", "w");
- gridStore.open(function(err, gridStore) {
+ new GridStore(db, "foobar3", "w").open(function(err, gridStore) {
gridStore.write('hello, world!', function(err, gridStore){});
gridStore.close(function(err, gridStore) {
gridStore = new GridStore(db, 'foobar3', "r");
@@ -109,14 +101,12 @@ db3.open(function(err, db) {
});
// Add some metadata
- gridStore = new GridStore(db, 'foobar3', "w+");
- gridStore.open(function(err, gridStore) {
+ new GridStore(db, 'foobar3', "w+").open(function(err, gridStore) {
gridStore.contentType = 'text/xml';
gridStore.metadata = {'a':1};
gridStore.close(function(err, gridStore) {
// Print the metadata
- gridStore = new GridStore(db, 'foobar3', "r");
- gridStore.open(function(err, gridStore) {
+ new GridStore(db, 'foobar3', "r").open(function(err, gridStore) {
console.log("contentType: " + gridStore.contentType);
console.log("uploadDate: " + gridStore.uploadDate);
console.log("chunkSize: " + gridStore.chunkSize);
@@ -133,8 +123,7 @@ db3.open(function(err, db) {
// collection: instead of gridfs.files and gridfs.chunks, here we use
// my_files.files and my_files.chunks
var gridStore = new GridStore(db, "foobar3", "w", {'content_type':'text/plain',
- 'metadata':{'a':1}, 'chunk_size': 1024*4, 'root':'my_files'});
- gridStore.open(function(err, gridStore) {
+ 'metadata':{'a':1}, 'chunk_size': 1024*4, 'root':'my_files'}).open(function(err, gridStore) {
gridStore.write('hello, world!', function(err, gridStore){});
gridStore.close(function() {
});
View
@@ -1,62 +1,61 @@
-var Db = require('../lib/mongodb').Db,
- Connection = require('../lib/mongodb').Connection,
- Server = require('../lib/mongodb').Server,
- mongo = require('../lib/mongodb');
+var Db = require('../lib/mongodb').Db
+ , Connection = require('../lib/mongodb').Connection
+ , Server = require('../lib/mongodb').Server
+ , 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;
console.log(">> Connecting to " + host + ":" + port);
-var db = new Db('node-mongo-examples', new Server(host, port, {}), {native_parser:true});
-db.open(function(err, db) {
+Db.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: ");
console.dir(result);
});
console.log(">> Creating collection test");
- db.collection('test', function(err, collection) {
- console.log("created: ");
- console.dir(collection);
+ var collection = db.collection('test');
+ console.log("created: ");
+ console.dir(collection);
- var objectCount = 100;
- var objects = [];
- var messages = ["hola", "hello", "aloha", "ciao"];
- console.log(">> Generate test data");
- for(var i = 0; i < objectCount; i++) {
- objects.push({'number':i, 'rndm':((5*Math.random()) + 1), 'msg':messages[parseInt(4*Math.random())]})
- }
- console.log("generated");
+ var objectCount = 100;
+ var objects = [];
+ var messages = ["hola", "hello", "aloha", "ciao"];
+ console.log(">> Generate test data");
+ for(var i = 0; i < objectCount; i++) {
+ objects.push({'number':i, 'rndm':((5*Math.random()) + 1), 'msg':messages[parseInt(4*Math.random())]})
+ }
+ console.log("generated");
- console.log(">> Inserting data (" + objects.length + ")");
- collection.insert(objects);
- console.log("inserted");
+ console.log(">> Inserting data (" + objects.length + ")");
+ collection.insert(objects, {w:0});
+ console.log("inserted");
+
+ console.log(">> Creating index")
+ collection.createIndex([['all'], ['_id', 1], ['number', 1], ['rndm', 1], ['msg', 1]], function(err, indexName) {
+ console.log("created index: " + indexName);
- console.log(">> Creating index")
- collection.createIndex([['all'], ['_id', 1], ['number', 1], ['rndm', 1], ['msg', 1]], function(err, indexName) {
- console.log("created index: " + indexName);
+ console.log(">> Gathering index information");
+
+ collection.indexInformation(function(err, doc) {
+ console.log("indexInformation: ");
+ console.dir(doc);
- console.log(">> Gathering index information");
-
- collection.indexInformation(function(err, doc) {
- console.log("indexInformation: ");
- console.dir(doc);
-
- console.log(">> Dropping index");
- collection.dropIndex('all_1__id_1_number_1_rndm_1_msg_1', function(err, result) {
- console.log("dropped: ");
- console.dir(result);
+ console.log(">> Dropping index");
+ collection.dropIndex('all_1__id_1_number_1_rndm_1_msg_1', function(err, result) {
+ console.log("dropped: ");
+ console.dir(result);
- console.log(">> Gathering index information");
- collection.indexInformation(function(err, doc) {
- console.log("indexInformation: ");
- console.dir(doc);
- console.log(">> Closing connection");
- db.close();
- });
- });
- });
- });
+ console.log(">> Gathering index information");
+ collection.indexInformation(function(err, doc) {
+ console.log("indexInformation: ");
+ console.dir(doc);
+ console.log(">> Closing connection");
+ db.close();
+ });
+ });
+ });
});
});
View
@@ -1,46 +1,43 @@
-var Db = require('../lib/mongodb').Db,
- Connection = require('../lib/mongodb').Connection,
- Server = require('../lib/mongodb').Server;
+var Db = require('../lib/mongodb').Db
+ , Connection = require('../lib/mongodb').Connection
+ , Server = require('../lib/mongodb').Server
+ , 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;
console.log("Connecting to " + host + ":" + port);
-var db = new Db('node-mongo-examples', new Server(host, port, {}), {native_parser:true});
-db.open(function(err, db) {
- db.collection('test', function(err, collection) {
+Db.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
- collection.remove(function(err, result) {
-
- // Insert 3 records
- for(var i = 0; i < 3; i++) {
- collection.insert({'a':i});
- }
-
- // Show collection names in the database
- db.collectionNames(function(err, names) {
- names.forEach(function(name) {
- console.dir(name);
- });
+ // Remove all existing documents in collection
+ collection.remove({}, {w:1}, function(err, result) {
+
+ // Insert 3 records
+ for(var i = 0; i < 3; i++) {
+ collection.insert({'a':i}, {w:0});
+ }
+
+ // Show collection names in the database
+ db.collectionNames(function(err, names) {
+ names.forEach(function(name) {
+ console.dir(name);
});
-
- // More information about each collection
- db.collectionsInfo(function(err, cursor) {
- cursor.toArray(function(err, items) {
- items.forEach(function(item) {
- console.dir(item);
- });
- });
- })
-
- // Index information
- db.createIndex('test', 'a', function(err, indexName) {
- db.indexInformation('test', function(err, doc) {
- console.dir(doc);
- collection.drop(function(err, result) {
- db.close();
- });
+ });
+
+ // More information about each collection
+ db.collectionsInfo().toArray(function(err, items) {
+ items.forEach(function(item) {
+ console.dir(item);
+ });
+ });
+
+ // Index information
+ db.createIndex('test', 'a', function(err, indexName) {
+ db.indexInformation('test', function(err, doc) {
+ console.dir(doc);
+ collection.drop(function(err, result) {
+ db.close();
});
});
});
Oops, something went wrong.

0 comments on commit 2cd47ab

Please sign in to comment.