Skip to content

Commit

Permalink
docs(api): fixing all examples in API documentation (#1616)
Browse files Browse the repository at this point in the history
Fix all the examples in the API documentation. Part of NODE-1218
  • Loading branch information
daprahamian authored Dec 18, 2017
1 parent ed80d73 commit 2c35d76
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 89 deletions.
15 changes: 9 additions & 6 deletions lib/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@ var toError = require('./utils').toError,
*
* **ADMIN Cannot directly be instantiated**
* @example
* var MongoClient = require('mongodb').MongoClient,
* test = require('assert');
* const MongoClient = require('mongodb').MongoClient;
* const test = require('assert');
* // Connection url
* var url = 'mongodb://localhost:27017/test';
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
*
* // Connect using MongoClient
* MongoClient.connect(url, function(err, db) {
* MongoClient.connect(url, function(err, client) {
* // Use the admin database for the operation
* var adminDb = db.admin();
* const adminDb = client.db(dbName).admin();
*
* // List all the available databases
* adminDb.listDatabases(function(err, dbs) {
* test.equal(null, err);
* test.ok(dbs.databases.length > 0);
* db.close();
* client.close();
* });
* });
*/
Expand Down
16 changes: 9 additions & 7 deletions lib/aggregation_cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ var inherits = require('util').inherits,
/**
* @fileOverview The **AggregationCursor** class is an internal class that embodies an aggregation cursor on MongoDB
* allowing for iteration over the results returned from the underlying query. It supports
* one by one document iteration, conversion to an array or can be iterated as a Node 0.10.X
* one by one document iteration, conversion to an array or can be iterated as a Node 4.X
* or higher stream
*
* **AGGREGATIONCURSOR Cannot directly be instantiated**
* @example
* var MongoClient = require('mongodb').MongoClient,
* test = require('assert');
* const MongoClient = require('mongodb').MongoClient;
* const test = require('assert');
* // Connection url
* var url = 'mongodb://localhost:27017/test';
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, db) {
* MongoClient.connect(url, function(err, client) {
* // Create a collection we want to drop later
* var col = db.collection('createIndexExample1');
* const col = client.db(dbName).collection('createIndexExample1');
* // Insert a bunch of documents
* col.insert([{a:1, b:1}
* , {a:2, b:2}, {a:3, b:3}
Expand All @@ -31,7 +33,7 @@ var inherits = require('util').inherits,
* col.aggregation({}, {cursor: {}}).toArray(function(err, items) {
* test.equal(null, err);
* test.equal(4, items.length);
* db.close();
* client.close();
* });
* });
* });
Expand Down
14 changes: 8 additions & 6 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,21 @@ var checkCollectionName = require('./utils').checkCollectionName,
*
* **COLLECTION Cannot directly be instantiated**
* @example
* var MongoClient = require('mongodb').MongoClient,
* test = require('assert');
* const MongoClient = require('mongodb').MongoClient;
* const test = require('assert');
* // Connection url
* var url = 'mongodb://localhost:27017/test';
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, db) {
* MongoClient.connect(url, function(err, client) {
* // Create a collection we want to drop later
* var col = db.collection('createIndexExample1');
* const col = client.db(dbName).collection('createIndexExample1');
* // Show that duplicate records got dropped
* col.find({}).toArray(function(err, items) {
* test.equal(null, err);
* test.equal(4, items.length);
* db.close();
* client.close();
* });
* });
*/
Expand Down
15 changes: 8 additions & 7 deletions lib/command_cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@ var inherits = require('util').inherits,
*
* **CommandCursor Cannot directly be instantiated**
* @example
* var MongoClient = require('mongodb').MongoClient,
* test = require('assert');
* const MongoClient = require('mongodb').MongoClient;
* const test = require('assert');
* // Connection url
* var url = 'mongodb://localhost:27017/test';
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, db) {
* MongoClient.connect(url, function(err, client) {
* // Create a collection we want to drop later
* var col = db.collection('listCollectionsExample1');
* const col = client.db(dbName).collection('listCollectionsExample1');
* // Insert a bunch of documents
* col.insert([{a:1, b:1}
* , {a:2, b:2}, {a:3, b:3}
* , {a:4, b:4}], {w:1}, function(err, result) {
* test.equal(null, err);
*
* // List the database collections available
* db.listCollections().toArray(function(err, items) {
* test.equal(null, err);
* db.close();
* client.close();
* });
* });
* });
Expand Down
17 changes: 9 additions & 8 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,31 @@ var inherits = require('util').inherits,
/**
* @fileOverview The **Cursor** class is an internal class that embodies a cursor on MongoDB
* allowing for iteration over the results returned from the underlying query. It supports
* one by one document iteration, conversion to an array or can be iterated as a Node 0.10.X
* one by one document iteration, conversion to an array or can be iterated as a Node 4.X
* or higher stream
*
* **CURSORS Cannot directly be instantiated**
* @example
* var MongoClient = require('mongodb').MongoClient,
* test = require('assert');
* const MongoClient = require('mongodb').MongoClient;
* const test = require('assert');
* // Connection url
* var url = 'mongodb://localhost:27017/test';
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, db) {
* MongoClient.connect(url, function(err, client) {
* // Create a collection we want to drop later
* var col = db.collection('createIndexExample1');
* const col = client.db(dbName).collection('createIndexExample1');
* // Insert a bunch of documents
* col.insert([{a:1, b:1}
* , {a:2, b:2}, {a:3, b:3}
* , {a:4, b:4}], {w:1}, function(err, result) {
* test.equal(null, err);
*
* // Show that duplicate records got dropped
* col.find({}).toArray(function(err, items) {
* test.equal(null, err);
* test.equal(4, items.length);
* db.close();
* client.close();
* });
* });
* });
Expand Down
12 changes: 7 additions & 5 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@ var illegalCommandFields = [
* @fileOverview The **Db** class is a class that represents a MongoDB Database.
*
* @example
* var MongoClient = require('mongodb').MongoClient,
* test = require('assert');
* const MongoClient = require('mongodb').MongoClient;
* const test = require('assert');
* // Connection url
* var url = 'mongodb://localhost:27017/test';
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, client) {
* // Get an additional db
* var testDb = client.db('test');
* db.close();
* const testDb = client.db('test');
* client.close();
* });
*/

Expand Down
21 changes: 11 additions & 10 deletions lib/gridfs/grid_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,25 @@
* found <a href="http://www.mongodb.org/display/DOCS/GridFS">here</a>.
*
* @example
* var MongoClient = require('mongodb').MongoClient,
* GridStore = require('mongodb').GridStore,
* ObjectID = require('mongodb').ObjectID,
* test = require('assert');
*
* const MongoClient = require('mongodb').MongoClient;
* const GridStore = require('mongodb').GridStore;
* const ObjectID = require('mongodb').ObjectID;
* const test = require('assert');
* // Connection url
* var url = 'mongodb://localhost:27017/test';
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, db) {
* var gridStore = new GridStore(db, null, "w");
* MongoClient.connect(url, function(err, client) {
* const db = client.db(dbName);
* const gridStore = new GridStore(db, null, "w");
* gridStore.open(function(err, gridStore) {
* gridStore.write("hello world!", function(err, gridStore) {
* gridStore.close(function(err, result) {
*
* // Let's read the file using object Id
* GridStore.read(db, result._id, function(err, data) {
* test.equal('hello world!', data);
* db.close();
* client.close();
* test.done();
* });
* });
Expand Down
14 changes: 8 additions & 6 deletions lib/mongo_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ var parse = require('./url_parser'),
* @fileOverview The **MongoClient** class is a class that allows for making Connections to MongoDB.
*
* @example
* var MongoClient = require('mongodb').MongoClient,
* test = require('assert');
* const MongoClient = require('mongodb').MongoClient;
* const test = require('assert');
* // Connection url
* var url = 'mongodb://localhost:27017/test';
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, db) {
* // Get an additional db
* db.close();
* MongoClient.connect(url, function(err, client) {
* const db = client.db(dbName);
* client.close();
* });
*/
var validOptionNames = [
Expand Down
12 changes: 0 additions & 12 deletions lib/topologies/mongos.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ var ServerCapabilities = require('./topology_base').ServerCapabilities,
* used to construct connections.
*
* **Mongos Should not be used, use MongoClient.connect**
* @example
* var Db = require('mongodb').Db,
* Mongos = require('mongodb').Mongos,
* Server = require('mongodb').Server,
* test = require('assert');
* // Connect using Mongos
* var server = new Server('localhost', 27017);
* var db = new Db('test', new Mongos([server]));
* db.open(function(err, db) {
* // Get an additional db
* db.close();
* });
*/

// Allowed parameters
Expand Down
12 changes: 0 additions & 12 deletions lib/topologies/replset.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ var Server = require('./server'),
* used to construct connections.
*
* **ReplSet Should not be used, use MongoClient.connect**
* @example
* var Db = require('mongodb').Db,
* ReplSet = require('mongodb').ReplSet,
* Server = require('mongodb').Server,
* test = require('assert');
* // Connect using ReplSet
* var server = new Server('localhost', 27017);
* var db = new Db('test', new ReplSet([server]));
* db.open(function(err, db) {
* // Get an additional db
* db.close();
* });
*/

// Allowed parameters
Expand Down
10 changes: 0 additions & 10 deletions lib/topologies/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ var CServer = require('mongodb-core').Server,
* used to construct connections.
*
* **Server Should not be used, use MongoClient.connect**
* @example
* var Db = require('mongodb').Db,
* Server = require('mongodb').Server,
* test = require('assert');
* // Connect using single Server
* var db = new Db('test', new Server('localhost', 27017););
* db.open(function(err, db) {
* // Get an additional db
* db.close();
* });
*/

// Allowed parameters
Expand Down

0 comments on commit 2c35d76

Please sign in to comment.