2018-10-25 20:29:56 +02:00
---
id: 587d7fb6367417b2b2512c06
title: Install and Set Up Mongoose
challengeType: 2
2019-08-05 09:17:33 -07:00
forumTopicId: 301540
2021-01-13 03:31:00 +01:00
dashedName: install-and-set-up-mongoose
2018-10-25 20:29:56 +02:00
---
2020-11-27 19:02:05 +01:00
# --description--
2019-12-02 20:12:27 -08:00
2020-11-03 14:22:55 +00:00
In this challenge, you will import the required projects, and connect to your Atlas database.
2019-12-02 20:12:27 -08:00
2020-11-27 19:02:05 +01:00
# --instructions--
2018-10-25 20:29:56 +02:00
2020-11-03 14:22:55 +00:00
Add `mongodb` and `mongoose` to the project’ s `package.json` . Then, require mongoose as `mongoose` in `myApp.js` . Store your MongoDB Atlas database URI in a private `.env` file as `MONGO_URI` . Surround the the URI with single or double quotes, and make sure no space exists between both the variable and the `=` , and the value and `=` . Connect to the database using the following syntax:
```js
mongoose.connect(< Your URI > , { useNewUrlParser: true, useUnifiedTopology: true });
```
2020-11-27 19:02:05 +01:00
# --hints--
2018-10-25 20:29:56 +02:00
2020-11-27 19:02:05 +01:00
"mongodb" dependency should be in package.json
2020-11-03 14:22:55 +00:00
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/file/package.json').then(
(data) => {
var packJson = JSON.parse(data);
assert.property(packJson.dependencies, 'mongodb');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
2018-10-25 20:29:56 +02:00
```
2020-11-27 19:02:05 +01:00
"mongoose" dependency should be in package.json
2018-10-25 20:29:56 +02:00
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/file/package.json').then(
(data) => {
var packJson = JSON.parse(data);
assert.property(packJson.dependencies, 'mongoose');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2020-11-03 14:22:55 +00:00
2020-11-27 19:02:05 +01:00
"mongoose" should be connected to a database
2018-10-25 20:29:56 +02:00
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/is-mongoose-ok').then(
(data) => {
assert.isTrue(data.isMongooseOk, 'mongoose is not connected');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2018-10-25 20:29:56 +02:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-10-25 20:29:56 +02:00
```js
2019-10-24 10:08:13 +05:30
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
2018-10-25 20:29:56 +02:00
```