Last Sync: 2022-08-11 12:30:04
This commit is contained in:
parent
47248ab73e
commit
7252616875
4 changed files with 27 additions and 16 deletions
22
Databases/MongoDB/Connect_to_db_with_Mongoose.md
Normal file
22
Databases/MongoDB/Connect_to_db_with_Mongoose.md
Normal file
|
@ -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));
|
||||||
|
```
|
|
@ -3,24 +3,11 @@ tags:
|
||||||
- Databases
|
- Databases
|
||||||
- mongo_db
|
- mongo_db
|
||||||
- node-js
|
- node-js
|
||||||
|
- mongoose
|
||||||
---
|
---
|
||||||
|
|
||||||
# Create collections and documents with 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
|
## 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.
|
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:
|
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
|
```js
|
||||||
const nodeCourse = new Course({
|
const pythonCourse = new Course({
|
||||||
name: "Python Course",
|
name: "Python Course",
|
||||||
author: "Terry Ogleton",
|
author: "Terry Ogleton",
|
||||||
tags: ["python"],
|
tags: ["python", "backend"],
|
||||||
isPublished: true,
|
isPublished: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -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.
|
A document is a container comprising key-value pairs in the manner of an object.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
The quick brown fox jumps
|
0
Databases/MongoDB/Querying_a_database.md
Normal file
0
Databases/MongoDB/Querying_a_database.md
Normal file
Loading…
Add table
Reference in a new issue