Using MongoDB from Node.js Module #57 Flashcards

1
Q

When starting a MongoDB project, what is the first step?

A

Create a new directory and run “npm init -y” to create a default package.json file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How is MongoDB installed into your project

A

By running “npm install mongodb” into your local project folder. There won’t be any files aside from the JSON and Node Modules yet.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the first step in your JavaScript to connect to a Mongo Database?

A
Store the client in a variable like so:
const mongo = require('mongodb').MongoClient
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How would you connect to the database?

A

Specify the URL to the database. Local development happens on localhost. First store the URL in a variable Example below:

const url = ‘mongodb://localhost:27017’

Then use the mongo.connect( ) method to get the reference to the MongoDB instance client with a try/catch statement:

try {
    const client = await mongo.connect(url, {
       useNewUrlParser: true,
       useUnifiedTopology: true
} )
} catch (err) {
console.error(err)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Do you need to specify the database name?

A

Yes. The url by itself is not enough. Make sure to declare a variable and store the db name inside of it for future reference. Example below:

const db = client.db( ‘kennel’ )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Now, how can you retrieve a specific collection? What if no collections exist?

A

You can retrieve a collection from the database using the db.collection( ) method. If the collection doesn’t yet exist, it’s created with the same line of code.

const collection = db.collection(‘dogs’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Now that we have created a place within the database to store our data, how would we insert some data into that place?

A

By calling the insertOne method. First we store the data we want to store in a variable, then we create an object with our data inside. Example below:

const result = await collection.insertOne({ name: ‘Roger’} )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Great, we have inserted one item into the Database, but what if we had a bunch of items we wanted to store all at once?

A

Start with the result variable, then instead of using insertOne( ), use insertMany( ), this time you need to pass an array with your objects inside. Example:

const result = await collection.insertMany( [ { name: ‘Togo’}, { name: ‘Syd’ } ] )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The database now has data. How can we perform a broad search to find all data?

A

To do a broad search and return all data embedded in the database use the .find( ) method and passing no parameters. Example below:

const items = await collection.find( ).toArray

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Returning all of the data could be a bit much. How can we narrow our search to a specific item?

A
Now we can add a parameter to our .find( ) method. Example:
const items = await collection.find( { name: 'Togo' } ).toArray( )

However if you know you’re only going to return one item you don’t need to apply the .toArray method

So the code would simply be like so:
const item = await collection.findOne( { name: 'Togo' } )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can we update an item/document in the database?

A
Use the .updateOne( ) method. Example:
const item = await collection.updateOne( { name: 'Togo" }, { '$set': {'name': 'Togo'})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can we delete a document from the database?

A
By using the deleteOne( ) method. Example:
const item = await collection.deletOne( {name: 'Togo' } )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Lastly how do we close down the connection?

A

By calling the .close( ) method on the client object

Example:
client.close( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly