CRUD in MongoDB
Creating a Database and Inserting Documents
MongoDB installs with a client app, a JavaScript-based shell, that allows us to interact with MongoDB directly.
Start the app in terminal by typing mongo
.
The app will load and change the prompt will change to >
.
List the shell's commands available: > help
.
Show the list of databases: > show dbs
.
Show the name of the currently active database: > db
.
Switch to a different database: > use [name of database to switch to]
.
Lets switch to the local
database: > use local
.
Show the collections of the current database > show collections
.
Creating a new Database
To create a new database in the Mongo Shell, we simply have to use the database. Lets create a database named myDB:
Inserting Data into a Collection
This how we can create and insert a document into a collection named people:
Using a collection for the first time creates it!
Adding Lots of New Documents
In a moment we'll practice querying our database, but let's get more data in there. Here are few more documents to put in your people collection. We can simply provide this array to the insert method and it will create a document for each object in the array.
Querying Documents
To list all documents in a collection, we use the find method on the collection without any arguments:
Again, unlike the rows in a relational database, our documents don't have to have the same fields!
More Specific Queries
We can also use the find()
method to query the collection by passing in an argument – a JS object containing criteria to query with.
There are a handful of special criteria variables we can use, too. Here's how we can use MongoDB's $gt
query operator to return all people documents with an age greater than 20:
MongoDB comes with a slew of built-in query operators we can use to write complex queries.
How would we write a query to retrieve people that are less than or equal to age 24?
This sorts our age query and sorts by name:
The "1" indicates ascending order.
This documentation provides more detail about reading data.
Updating Data
In MongoDB, we use the update()
method of collections by specifying the update criteria (like we did with find()
), and use the $set
action to set the new value.
By default update()
will only modify a single document. However, with the multi
option, we can update all of the documents that match the query.
We used the $inc
update operator to increase the existing value.
Here is the list of Update Operators available.
Removing Data
We use the remove()
method to data from collections.
If you want to completely remove a collection, including all of its indexes, use db.[name of the collection].drop()
.
Call remove({})
on the collection to remove all docs from a collection. Note: all documents will match the "empty" criteria.
Otherwise, specify a criteria to remove all documents that match it:
Last updated