Using MongoDB from Node.js Module #57 Flashcards
When starting a MongoDB project, what is the first step?
Create a new directory and run “npm init -y” to create a default package.json file.
How is MongoDB installed into your project
By running “npm install mongodb” into your local project folder. There won’t be any files aside from the JSON and Node Modules yet.
What is the first step in your JavaScript to connect to a Mongo Database?
Store the client in a variable like so: const mongo = require('mongodb').MongoClient
How would you connect to the database?
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) }
Do you need to specify the database name?
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’ )
Now, how can you retrieve a specific collection? What if no collections exist?
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’)
Now that we have created a place within the database to store our data, how would we insert some data into that place?
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’} )
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?
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’ } ] )
The database now has data. How can we perform a broad search to find all data?
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
Returning all of the data could be a bit much. How can we narrow our search to a specific item?
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 can we update an item/document in the database?
Use the .updateOne( ) method. Example: const item = await collection.updateOne( { name: 'Togo" }, { '$set': {'name': 'Togo'})
How can we delete a document from the database?
By using the deleteOne( ) method. Example: const item = await collection.deletOne( {name: 'Togo' } )
Lastly how do we close down the connection?
By calling the .close( ) method on the client object
Example:
client.close( )