diff --git a/README.md b/README.md index db5cde4..f7a98f5 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,23 @@ This repository will walk you through the process of quickly getting started using Node.js and MongoDB together. -[Lauren Schaefer](https://github.com/ljhaywar) is actively working to add files to this repo and drafting a blog series that will walk you through the code. She will be updating this readme as she publishes blog posts. +This branch uses MongoDB 4.0, MongoDB Node.js Driver 3.3.2, and Node.js 10.16.3. To see the latest version, visit the [master branch](https://github.com/mongodb-developer/nodejs-quickstart). ## Topics -* How to get connected to your database: [blog post](https://www.mongodb.com/blog/post/quick-start-nodejs-mongodb--how-to-get-connected-to-your-database) | [code example](connection.js) -* Creating documents: [blog post](https://www.mongodb.com/blog/post/quick-start-nodejs--mongodb--how-to-create-documents) | [code example](create.js) -* Reading documents: [blog post](https://www.mongodb.com/blog/post/quick-start-nodejs--mongodb--how-to-read-documents) | [code example](read.js) -* Updating documents: [blog post](https://www.mongodb.com/blog/post/quick-start-nodejs--mongodb--how-to-update-documents) | [code example](update.js) -* Deleting documents: [blog post](https://www.mongodb.com/blog/post/quick-start-nodejs--mongodb--how-to-delete-documents) | [code example](delete.js) -* The aggregation framework: [blog post](https://www.mongodb.com/blog/post/quick-start-nodejs--mongodb--how-to-analyze-data-using-the-aggregation-framework) | [code example](aggregation.js) -* Transactions: [blog post](https://www.mongodb.com/blog/post/quick-start-nodejs--mongodb--how-to-implement-transactions) | [code example](transaction.js) -* Change streams: [blog post](https://www.mongodb.com/blog/post/node-js-change-streams-and-triggers) | [code example](changeStreams.js) +* How to get connected to your database: [blog post](https://developer.mongodb.com/quickstart/node-connect-mongodb/) | [code example](connection.js) +* Creating documents: [blog post](https://developer.mongodb.com/quickstart/node-crud-tutorial/) | [code example](create.js) +* Reading documents: [blog post](https://developer.mongodb.com/quickstart/node-crud-tutorial/) | [code example](read.js) +* Updating documents: [blog post](https://developer.mongodb.com/quickstart/node-crud-tutorial/) | [code example](update.js) +* Deleting documents: [blog post](https://developer.mongodb.com/quickstart/node-crud-tutorial/) | [code example](delete.js) +* The aggregation framework: [blog post](https://developer.mongodb.com/quickstart/node-aggregation-framework/) | [code example](aggregation.js) +* Transactions: [blog post](https://developer.mongodb.com/quickstart/node-transactions/) | [code example](transaction.js) +* Change streams: [blog post](https://developer.mongodb.com/quickstart/nodejs-change-streams-triggers/) | [code example](changeStreams.js) ## Related Videos [How to Perform the CRUD Operations Using MongoDB & Node.js](https://youtu.be/ayNI9Q84v8g) + +## Questions? + +Questions about this repo or how to use MongoDB and Node.js together? Ask them in the [MongoDB Community](https://community.mongodb.com). diff --git a/create.js b/create.js index 3d7f8bb..b58469e 100644 --- a/create.js +++ b/create.js @@ -3,13 +3,13 @@ const {MongoClient} = require('mongodb'); async function main(){ /** * Connection URI. Update , , and to reflect your cluster. - * See http://bit.ly/NodeDocs_lauren for more details + * See https://docs.mongodb.com/drivers/node/ for more details */ const uri = "mongodb+srv://:@/sample_airbnb?retryWrites=true&w=majority"; /** * The Mongo Client you will use to interact with your database - * See bit.ly/Node_MongoClient for more details + * See https://mongodb.github.io/node-mongodb-native/3.3/api/MongoClient.html for more details */ const client = new MongoClient(uri); @@ -68,7 +68,7 @@ main().catch(console.error); * @param {Object} newListing The new listing to be added */ async function createListing(client, newListing){ - // See http://bit.ly/Node_InsertOne for the insertOne() docs + // See https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#insertOne for the insertOne() docs const result = await client.db("sample_airbnb").collection("listingsAndReviews").insertOne(newListing); console.log(`New listing created with the following id: ${result.insertedId}`); } @@ -79,7 +79,7 @@ async function createListing(client, newListing){ * @param {Object[]} newListings The new listings to be added */ async function createMultipleListings(client, newListings){ - // See http://bit.ly/Node_InsertMany for the insertMany() docs + // See https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#insertMany for the insertMany() docs const result = await client.db("sample_airbnb").collection("listingsAndReviews").insertMany(newListings); console.log(`${result.insertedCount} new listing(s) created with the following id(s):`); diff --git a/delete.js b/delete.js index 62cc95a..bc04225 100644 --- a/delete.js +++ b/delete.js @@ -3,13 +3,13 @@ const { MongoClient } = require('mongodb'); async function main() { /** * Connection URI. Update , , and to reflect your cluster. - * See http://bit.ly/NodeDocs_lauren for more details + * See https://docs.mongodb.com/drivers/node/ for more details */ const uri = "mongodb+srv://:@/sample_airbnb?retryWrites=true&w=majority"; /** * The Mongo Client you will use to interact with your database - * See bit.ly/Node_MongoClient for more details + * See https://mongodb.github.io/node-mongodb-native/3.3/api/MongoClient.html for more details */ const client = new MongoClient(uri); @@ -50,12 +50,11 @@ main().catch(console.error); /** * Delete an Airbnb listing with the given name. * Note: If more than one listing has the same name, only the first listing the database finds will be deleted. - * It's best to use deleteOne when querying on fields that are guaranteed to be unique. * @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database * @param {string} nameOfListing The name of the listing you want to delete */ async function deleteListingByName(client, nameOfListing) { - // See http://bit.ly/Node_deleteOne for the deleteOne() docs + // See https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#deleteOne for the deleteOne() docs const result = await client.db("sample_airbnb").collection("listingsAndReviews").deleteOne({ name: nameOfListing }); console.log(`${result.deletedCount} document(s) was/were deleted.`); } @@ -66,7 +65,7 @@ async function deleteListingByName(client, nameOfListing) { * @param {Date} date The date to check the last_scraped property against */ async function deleteListingsScrapedBeforeDate(client, date) { - // See http://bit.ly/Node_deleteMany for the deleteMany() docs + // See https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#deleteMany for the deleteMany() docs const result = await client.db("sample_airbnb").collection("listingsAndReviews").deleteMany({ "last_scraped": { $lt: date } }); console.log(`${result.deletedCount} document(s) was/were deleted.`); } @@ -75,12 +74,11 @@ async function deleteListingsScrapedBeforeDate(client, date) { * Print information indicating if a listing with the given name exists. * If a listing has the 'last_scraped' field, print that as well. * Note: If more than one listing has the same name, only the first listing the database finds will be printed. - * It's best to use findOne when querying on fields that are guaranteed to be unique. * @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database * @param {String} nameOfListing The name of the listing you want to find */ async function printIfListingExists(client, nameOfListing) { - // See http://bit.ly/Node_findOne for the findOne() docs + // See https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#findOne for the findOne() docs const result = await client.db("sample_airbnb").collection("listingsAndReviews").findOne({ name: nameOfListing }); if (result) { diff --git a/read.js b/read.js index 1369a13..eff4755 100644 --- a/read.js +++ b/read.js @@ -3,13 +3,13 @@ const { MongoClient } = require('mongodb'); async function main() { /** * Connection URI. Update , , and to reflect your cluster. - * See http://bit.ly/NodeDocs_lauren for more details + * See https://docs.mongodb.com/drivers/node/ for more details */ const uri = "mongodb+srv://:@/sample_airbnb?retryWrites=true&w=majority"; /** * The Mongo Client you will use to interact with your database - * See bit.ly/Node_MongoClient for more details + * See https://mongodb.github.io/node-mongodb-native/3.3/api/MongoClient.html for more details */ const client = new MongoClient(uri); @@ -41,12 +41,11 @@ main().catch(console.error); /** * Print an Airbnb listing with the given name * Note: If more than one listing has the same name, only the first listing the database finds will be printed. - * It's best to use findOne when querying on fields that are guaranteed to be unique. * @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database * @param {String} nameOfListing The name of the listing you want to find */ async function findOneListingByName(client, nameOfListing) { - // See http://bit.ly/Node_findOne for the findOne() docs + // See https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#findOne for the findOne() docs const result = await client.db("sample_airbnb").collection("listingsAndReviews").findOne({ name: nameOfListing }); if (result) { @@ -73,7 +72,7 @@ async function findListingsWithMinimumBedroomsBathroomsAndMostRecentReviews(clie maximumNumberOfResults = Number.MAX_SAFE_INTEGER } = {}) { - // See http://bit.ly/Node_find for the find() docs + // See https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#find for the find() docs const cursor = client.db("sample_airbnb").collection("listingsAndReviews") .find({ bedrooms: { $gte: minimumNumberOfBedrooms }, diff --git a/template.js b/template.js index af74dea..20b2bb0 100644 --- a/template.js +++ b/template.js @@ -3,13 +3,13 @@ const { MongoClient } = require('mongodb'); async function main() { /** * Connection URI. Update , , and to reflect your cluster. - * See http://bit.ly/NodeDocs_lauren for more details + * See https://docs.mongodb.com/drivers/node/ for more details */ const uri = "mongodb+srv://:@/sample_airbnb?retryWrites=true&w=majority"; /** * The Mongo Client you will use to interact with your database - * See bit.ly/Node_MongoClient for more details + * See https://mongodb.github.io/node-mongodb-native/3.3/api/MongoClient.html for more details */ const client = new MongoClient(uri); diff --git a/update.js b/update.js index 8156ea0..57a9c0f 100644 --- a/update.js +++ b/update.js @@ -3,13 +3,13 @@ const { MongoClient } = require('mongodb'); async function main() { /** * Connection URI. Update , , and to reflect your cluster. - * See http://bit.ly/NodeDocs_lauren for more details + * See https://docs.mongodb.com/drivers/node/ for more details */ const uri = "mongodb+srv://:@/sample_airbnb?retryWrites=true&w=majority"; /** * The Mongo Client you will use to interact with your database - * See bit.ly/Node_MongoClient for more details + * See https://mongodb.github.io/node-mongodb-native/3.3/api/MongoClient.html for more details */ const client = new MongoClient(uri); @@ -56,13 +56,12 @@ main().catch(console.error); /** * Update an Airbnb listing with the given name * Note: If more than one listing has the same name, only the first listing the database finds will be updated. - * It's best to use updateOne when querying on fields that are guaranteed to be unique. * @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database * @param {string} nameOfListing The name of the listing you want to update * @param {object} updatedListing An object containing all of the properties to be updated for the given listing */ async function updateListingByName(client, nameOfListing, updatedListing) { - // See http://bit.ly/Node_updateOne for the updateOne() docs + // See https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#updateOne for the updateOne() docs const result = await client.db("sample_airbnb").collection("listingsAndReviews").updateOne({ name: nameOfListing }, { $set: updatedListing }); console.log(`${result.matchedCount} document(s) matched the query criteria.`); @@ -74,7 +73,6 @@ async function updateListingByName(client, nameOfListing, updatedListing) { * If a listing with the given name exists, it will be updated. * If a listing with the given name does not exist, it will be inserted. * Note: If more than one listing has the same name, only the first listing the database finds will be updated. - * It's best to use updateOne when querying on fields that are guaranteed to be unique. * Note: For educational purposes, we have split the update and upsert functionality into separate functions. * Another option is to have a single function where a boolean param indicates if the update should be an upsert. * @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database @@ -82,7 +80,7 @@ async function updateListingByName(client, nameOfListing, updatedListing) { * @param {object} updatedListing An object containing all of the properties to be upserted for the given listing */ async function upsertListingByName(client, nameOfListing, updatedListing) { - // See http://bit.ly/Node_updateOne for the updateOne() docs + // See https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#updateOne for the updateOne() docs const result = await client.db("sample_airbnb").collection("listingsAndReviews").updateOne({ name: nameOfListing }, { $set: updatedListing }, { upsert: true }); console.log(`${result.matchedCount} document(s) matched the query criteria.`); @@ -98,7 +96,7 @@ async function upsertListingByName(client, nameOfListing, updatedListing) { * @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database */ async function updateAllListingsToHavePropertyType(client) { - // See http://bit.ly/Node_updateMany for the updateMany() docs + // See https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#updateMany for the updateMany() docs const result = await client.db("sample_airbnb").collection("listingsAndReviews").updateMany({ property_type: { $exists: false } }, { $set: { property_type: "Unknown" } }); console.log(`${result.matchedCount} document(s) matched the query criteria.`); console.log(`${result.modifiedCount} document(s) was/were updated.`); @@ -107,12 +105,11 @@ async function updateAllListingsToHavePropertyType(client) { /** * Print an Airbnb listing with the given name * Note: If more than one listing has the same name, only the first listing the database finds will be printed. - * It's best to use findOne when querying on fields that are guaranteed to be unique. * @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database * @param {String} nameOfListing The name of the listing you want to find */ async function findListingByName(client, nameOfListing) { - // See http://bit.ly/Node_findOne for the findOne() docs + // See https://mongodb.github.io/node-mongodb-native/3.3/api/Collection.html#findOne for the findOne() docs const result = await client.db("sample_airbnb").collection("listingsAndReviews").findOne({ name: nameOfListing }); if (result) {