MongoDB Flashcards
Intro to MongoDB
Install MongoDB on Mac
Follow directions on Mongodb
MongoDB CRUD Operations
Create
Create a database
terminal: use
Create a collection / Insert new items into a collection
db..insertOne()
db..insertMany()
If the collection doesn’t exist, then a new one will be created
Read / Query terminal: show collections Shows all the collections db..find() Displays all the data in the collection db..find( , )
Query Examples
Displays all the data in the collection that meets the query criteria
Example of Query:
{name : “Pencil”}
{price: {$gt: 1}}
Example of Projection:
{name : 1, address: 1}
1 means true
0 means false
We can dictate which fields we want back from the query
So in this case, where the data has a name and an address
Update
db..updateOne(, )
The data to update
{$set: {: }}
Delete
db. .deleteOne()
db. .deleteMany()
Relationships
One to Many Relationships are great for NoSQL - MongoDB Example: Product Review 1 Review 2 Review 3 ...
Mongo Shell
terminal: mongod Starts up the mongo server When you see “waiting for connections on port 27017” then the server has started terminal: mongo Starts up the mongo shell Troubleshoot: sudo killall -15 mongod If unable to start up the server using mongod
MongoDB w/ Node JS
2 ways to incorporate MongoDB app with Node.js
1. Native MongoDB Driver
Install a MongoDB driver from the documentation
One that corresponds to the programming language
In this case, Node.js MongoDB driver
npm install mongodb
2. mongoose
ODM
Object Data Modeling library
Most popular package to work with MongoDB and Node.JS