This guide is meant to help you get started with MongoDB. First install MongoDB on your local machine, run it, and try a few queries in from the turorial below. We will use the same queries in the back-end.
These steps are pulled directly from the guide above.
Prepare for install.
xcode-select --install
brew update
Install
brew tap mongodb/brew
brew install [email protected]
Start the mongodb service.
brew services start [email protected]
Run mongo
to connect to the db. Inside the mongo shell run use test
to switch to the test DB. Then add some test data.
db.inventory.insertOne(
{
"name" : "Tigerlilly",
"animal" : "cat",
"sound" : "meow",
"tags" : ["mammal"],
"features" : {
"color": "orange",
"eyes": "green"
},
}
)
myCursor = db.inventory.find({})
db.inventory.insertMany([
{ "name": "Dr. Cat", "animal" : "cat", "sound" : "meow", "tags" : ["mammal"], "features" : { "color": "grey", "eyes": "brown" }},
{ "name": "Vader", "animal" : "cat", "sound" : "meow", "tags" : ["mammal"], "features" : { "color": "black", "eyes": "yellow" }},
{ "name": "Jadis", "animal" : "cat", "sound" : "meow", "tags" : ["mammal"], "features" : { "color": "black & white", "eyes": "yellow" }},
])
myCursor = db.inventory.find({ name: "Vader" })
And for nested values
myCursor = db.inventory.find({ "features.color": "black" })
There are 20 items per "page" of results. To fetch multiple pages.
while (myCursor.hasNext()) {
print(tojson(myCursor.next()));
}
myCursor = db.inventory.find({ "features.color": { "$in": ["black", "orange"] } })
There are numerical filters too.
myCursor = db.inventory.find({ "numberValue": { "$lt": 5 } })
Here is the full list of query operators
Updates follow this format
db.inventory.updateOne(
{ ...query },
{ ...update}
)
For example
db.inventory.updateOne(
{ name: "Dr. Cat" },
{
$set: { "features.color": "grey stripes" }
}
)
You can update multiple records using updateMany
instead.
Deletes follow this format
db.inventory.deleteMany({
...query
})
For example
db.inventory.deleteMany({
name: "Dr. Cat".
})
You can target one record using deleteOne
as well.