MongoDB and Mongoose, first challenge, how do i get the name of my default database?

Replace with the name of the database that connections will use by default.

That’s what I have to do in my URI link in order to pass the challenge.

It might be that this answer is quite obvious and I am just missing it. Somehow this whole backend thing is a bit hard to get comfortable with at the moment (although once I understand it, it isn’t really that hard) so if you have any additional advice on what you can recommend for the absolute beginning of backend programming, it’s greatly appreciated.

Hello there,

Do not worry about feeling a little confused, or lost with what to do; eventually, this starts falling into place, you move on, and repeat.

For this specific problem, provided it is a MongoDB (Atlas) URI, you can actually put almost anything in the collection string. That is, if it does not already exist on your database, then a new one with the given string will be created.

When I did those challenges, I did not understand this well, so I used test as the name for most of them.

In general, if you want to know about the specifics like this, then the official docs for MongoDB and Atlas would be the best place to look for extra information.

Hope this helps

1 Like

Thanks for the answer! I was already assuming that I would get used to it, but it sure is comforting to hear this be confirmed. On a different note, it seems, that the problem in passing the challenge was actually not coming from that, or to be more precise, this wasn’t the main/only problem.
I don’t really understand the whole .env thing yet.

I can just type this into myApp.js: process.env.ANYTHING=“something”
and that works to pass the challenge, but as I understand, the main point of .env is, that it’s private and if i type the previous example into myApp.js, it will no longer be private.

So is the solution to this just to create a new file and type it in there?
(in this task a “sample.env” file was provided.). But how exactly would I access that?

Yes, exactly this.

The sample.env file is a convention many devs use to share non-secret environment variables. You cannot access the environment variables in a sample.env file. So, you need to copy the contents of sample.env into .env.

It is bad practice to share a .env file, so you are expected to always create one, and add it to a .gitignore file (do not worry much about this now, if you have not heard of it - research if you want)

So, the idea is you should:

  • Create a .env file
  • Copy the contents (if any) of sample.env into .env
  • Add your secret environment variables into .env (e.g. Database URI)
  • Access the variables with process.env.VARIABLE_NAME (often, you will need to use a package like dotenv to be able to use process.env)

Some info about using .env file:

  • It is nothing more than a plain text file
  • It is line-delimeted
  • The above means whatever you put on one line is the name and value of a variable.

So, if you say MY_VARIABLE = VALUE; this will be the case:

console.log(process.env.MY_VARIABLE) // undefined
console.log(process.env["MY_VARIABLE "]) // " VALUE;"
// Notice the added spaces and the fact that the `value` includes the `;`

EDIT: What I mean with the above is it is a poor use of .env variables, and the correct way is:
.env file

MY_VARIABLE=VALUE
ANOTHER_VARIABLE=some other text
FOR_OLD_VERSIONS="some more text"

I hope this clarifies.

2 Likes

Absolutely perfect!! Thanks for the great help, it’s really appreciated!
So I assume the following is correct:

The way to create a .env file is just by simply creating a new file and calling it ‘.env’
This will “automatically” make it private. (if it is to be private on github, add “.env” to the .gitignore file)

Yes. In workspaces like Repl.it, Glitch, and CodeSandbox, the .env is made private, unless you invite people to be a part of the project (and tick a box - for some)

Glad this has helped

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.