Skip to content

Commit

Permalink
docs(tutorial): Updating docs for 3.0.0 (#1605)
Browse files Browse the repository at this point in the history
* docs(quickstart): updating quickstart for 3.0.0

* docs(connecting): updating connection tutorials for 3.0.0

Updating the tutorial pages for Connecting to MongoDB, Authentication,
and SSL Settings

* docs(collation): updating collation tutorial for 3.0

* docs(collection): update collection tutorial for 3.0

* docs(createIndexes): updating create-indexes tutorial for 3.0

* docs(projection): updating projection tutorial for 3.0

* docs(aggregation): updating aggregation tutorial for 3.0

* docs(textsearch): updating text search tutorial for 3.0

* docs(geospatial): updating geospatial search tutorial for 3.0

* docs(databaseCommands): updating database commands tutorial for 3.0

* docs(gridfs): updating gridfs tutorials for 3.0

* docs(crud): updating crud tutorial for 3.0

Part of NODE-1212
  • Loading branch information
daprahamian authored Dec 8, 2017
1 parent 847f9b3 commit 912fb5e
Show file tree
Hide file tree
Showing 24 changed files with 637 additions and 627 deletions.
101 changes: 65 additions & 36 deletions docs/reference/content/quick-start/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ Add to **app.js** the following function which uses the **insertMany**
method to add three documents to the **documents** collection.

```js
var insertDocuments = function(db, callback) {
const insertDocuments = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
const collection = db.collection('documents');
// Insert some documents
collection.insertMany([
{a : 1}, {a : 2}, {a : 3}
Expand All @@ -98,18 +98,24 @@ The **insert** command returns an object with the following fields:
Add the following code to call the **insertDocuments** function:

```js
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");

const db = client.db(dbName);

insertDocuments(db, function() {
db.close();
client.close();
});
});
```
Expand All @@ -132,9 +138,9 @@ Find All Documents
Add a query that returns all the documents.

```js
var findDocuments = function(db, callback) {
const findDocuments = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
const collection = db.collection('documents');
// Find some documents
collection.find({}).toArray(function(err, docs) {
assert.equal(err, null);
Expand All @@ -159,19 +165,25 @@ is run repeatedly the number of documents in the collection will change.
-->

```js
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("Connected correctly to server");

const db = client.db(dbName);

insertDocuments(db, function() {
findDocuments(db, function() {
db.close();
client.close();
});
});
});
Expand All @@ -183,16 +195,16 @@ Find Documents with a Query Filter
Add a query filter to find only documents which meet the query criteria.

```js
var findDocuments = function(db, callback) {
const findDocuments = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
const collection = db.collection('documents');
// Find some documents
collection.find({'a': 3}).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
console.log(docs);
callback(docs);
});
});
}
```

Expand All @@ -203,9 +215,9 @@ Update a document
The following operation updates a document in the **documents** collection.

```js
var updateDocument = function(db, callback) {
const updateDocument = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
const collection = db.collection('documents');
// Update document where a is 2, set b equal to 1
collection.updateOne({ a : 2 }
, { $set: { b : 1 } }, function(err, result) {
Expand All @@ -220,19 +232,25 @@ var updateDocument = function(db, callback) {
The method updates the first document where the field **a** is equal to **2** by adding a new field **b** to the document set to **1**. Next, update the callback function from **MongoClient.connect** to include the update method.

```js
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");

const db = client.db(dbName);

insertDocuments(db, function() {
updateDocument(db, function() {
db.close();
client.close();
});
});
});
Expand All @@ -243,9 +261,9 @@ Remove a document
Remove the document where the field **a** is equal to **3**.

```js
var removeDocument = function(db, callback) {
const removeDocument = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
const collection = db.collection('documents');
// Delete document where a is 3
collection.deleteOne({ a : 3 }, function(err, result) {
assert.equal(err, null);
Expand All @@ -259,20 +277,26 @@ var removeDocument = function(db, callback) {
Add the new method to the **MongoClient.connect** callback function.

```js
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");

const db = client.db(dbName);

insertDocuments(db, function() {
updateDocument(db, function() {
removeDocument(db, function() {
db.close();
client.close();
});
});
});
Expand All @@ -293,19 +317,24 @@ performance. The following function creates an index on the **a** field in the
Add the ``indexCollection`` method to your app:

```js
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
const url = 'mongodb://localhost:27017';

const dbName = 'myproject';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");

const db = client.db(dbName);

insertDocuments(db, function() {
indexCollection(db, function() {
db.close();
client.close();
});
});
});
Expand Down
70 changes: 25 additions & 45 deletions docs/reference/content/tutorials/aggregation.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,20 @@ sample dataset to find a list of restaurants located in the Bronx,
grouped by restaurant category.

```js
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
var url = 'mongodb://localhost:27017/test';
{{% myproject-connect %}}

MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
simplePipeline(db, function() {
db.close();
client.close();
});
});

var simplePipeline = function(db, callback) {
var collection = db.collection( 'restaurants' );
collection.aggregate(
function simplePipeline(db, callback) {
const collection = db.collection( 'restaurants' );
collection.aggregate(
[ { '$match': { "borough": "Bronx" } },
{ '$unwind': '$categories'},
{ '$group': { '_id': "$categories", 'Bronx restaurants': { '$sum': 1 } } }
],
{ '$group': { '_id': "$categories", 'Bronx restaurants': { '$sum': 1 } } }
],
function(err, results) {
assert.equal(err, null);

Expand Down Expand Up @@ -83,19 +79,15 @@ find the total number of documents which have the exact array
``[ 'Chinese', 'Seafood' ]`` in the ``categories`` field.

```js
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
var url = 'mongodb://localhost:27017/test';
{{% myproject-connect %}}

MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
simpleCount(db, function() {
db.close();
client.close();
});
});

var simpleCount = function(db, callback) {
var collection = db.collection( 'restaurants' );
function simpleCount(db, callback) {
const collection = db.collection( 'restaurants' );
collection.count({ 'categories': [ 'Chinese', 'Seafood' ] },
function(err, result) {
assert.equal(err, null);
Expand All @@ -120,24 +112,19 @@ The example groups the results by number of stars where the ``categories``
array is ``['Peruvian']``.

```js
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
var url = 'mongodb://localhost:27017/test';
{{% myproject-connect %}}

MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
simpleGroup(db, function() {
db.close();
client.close();
});
});

var simpleGroup = function(db, callback) {
var collection = db.collection( 'restaurants' );
function simpleGroup(db, callback) {
const collection = db.collection( 'restaurants' );
collection.group( ['stars'],
{ 'categories': ['Peruvian'] },
{ 'total': 0 },
"function ( curr, result ) { result.total++ }",

"function ( curr, result ) { result.total++ }",
function(err, result) {
assert.equal(err, null);
console.log(result)
Expand All @@ -156,26 +143,19 @@ The following example returns a list of unique values for the
``categories`` field in the ``restaurants`` collection:

```js
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
var url = 'mongodb://localhost:27017/test';
{{% myproject-connect %}}

MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
simpleDistinct(db, function() {
db.close();
client.close();
});
});

var simpleDistinct = function(db, callback) {
var collection = db.collection( 'restaurants' );
collection.distinct( 'categories',

function(err, result) {
assert.equal(err, null);
console.log(result)
callback(result);
}
);
function simpleDistinct(db, callback) {
const collection = db.collection( 'restaurants' );
collection.distinct( 'categories', function(err, result) {
assert.equal(err, null);
console.log(result)
callback(result);
});
}
```
Loading

0 comments on commit 912fb5e

Please sign in to comment.