From 7252616875bdc31a02ed1337a10cff5fd1fde1b4 Mon Sep 17 00:00:00 2001 From: tactonbishop Date: Thu, 11 Aug 2022 12:30:04 +0100 Subject: [PATCH] Last Sync: 2022-08-11 12:30:04 --- .../MongoDB/Connect_to_db_with_Mongoose.md | 22 +++++++++++++++++++ ...collections_and_documents_with_Mongoose.md | 19 +++------------- Databases/MongoDB/Introduction.md | 2 ++ Databases/MongoDB/Querying_a_database.md | 0 4 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 Databases/MongoDB/Connect_to_db_with_Mongoose.md create mode 100644 Databases/MongoDB/Querying_a_database.md diff --git a/Databases/MongoDB/Connect_to_db_with_Mongoose.md b/Databases/MongoDB/Connect_to_db_with_Mongoose.md new file mode 100644 index 0000000..68d3c79 --- /dev/null +++ b/Databases/MongoDB/Connect_to_db_with_Mongoose.md @@ -0,0 +1,22 @@ +--- +tags: +- Databases +- mongo_db +- node-js +- mongoose +--- + +# Connect to a database with Mongoose + +Now that we have installed and configured MongoDB, we need to connect to it via Node.js. Mongoose is a simple API for interacting with a Mongo database via Node. + +With Mongoose installed we can connect to a database. We don't have any Mongo databases yet beyond the defaults but the following Mongoose connection logic will create and connect to a new database called `playground`: + +Providing the Mongo server is running (execture `mongod`), we will see the confirmation message in the console. + +```js +mongoose + .connect("mongodb://127.0.0.1/playground") + .then(() => console.log("Connected to MongoDB")) + .catch((err) => console.error(err)); +``` diff --git a/Databases/MongoDB/Create_collections_and_documents_with_Mongoose.md b/Databases/MongoDB/Create_collections_and_documents_with_Mongoose.md index 189eb8a..64e2b34 100644 --- a/Databases/MongoDB/Create_collections_and_documents_with_Mongoose.md +++ b/Databases/MongoDB/Create_collections_and_documents_with_Mongoose.md @@ -3,24 +3,11 @@ tags: - Databases - mongo_db - node-js +- mongoose --- # Create collections and documents with Mongoose -## Connecting to our database with Mongoose -Now that we have installed and configured MongoDB, we need to connect to it via Node.js. Mongoose is a simple API for interacting with a Mongo database via Node. - -With Mongoose installed we can connect to a database. We don't have any Mongo databases yet beyond the defaults but the following Mongoose connection logic will create and connect to a new database called `playground`: - -Providing the Mongo server is running (execture `mongod`), we will see the confirmation message in the console. - -```js -mongoose - .connect("mongodb://127.0.0.1/playground") - .then(() => console.log("Connected to MongoDB")) - .catch((err) => console.error(err)); -``` - ## Collections and documents In order start adding collections and documents to our database, we use Mongoose's schema structure. This is specific to Mongoose and is not a structure that is a part of Mongo in general. @@ -77,10 +64,10 @@ Having created a database, connected to it with Mongoose, and created a model we We have our database (`playground`) and collection (`courses`) established. We now need to add documents to our collection. We will move our previous code into a function since this will be an asynchronous process: ```js -const nodeCourse = new Course({ +const pythonCourse = new Course({ name: "Python Course", author: "Terry Ogleton", - tags: ["python"], + tags: ["python", "backend"], isPublished: true, }); diff --git a/Databases/MongoDB/Introduction.md b/Databases/MongoDB/Introduction.md index a7e3e93..701a139 100644 --- a/Databases/MongoDB/Introduction.md +++ b/Databases/MongoDB/Introduction.md @@ -18,3 +18,5 @@ Although Mongo is not a relational database it has a structure that we can under A document is a container comprising key-value pairs in the manner of an object. ![](/img/mongo-db-structure.svg) + +The quick brown fox jumps \ No newline at end of file diff --git a/Databases/MongoDB/Querying_a_database.md b/Databases/MongoDB/Querying_a_database.md new file mode 100644 index 0000000..e69de29