Install MongoDB on MacOS with Homebrew and first steps
Content Index
- Software required to install install MongoDB
- Install Homebrew
- Install MongoDB Homebrew Tap
- This is a custom Homebrew Tap (package) for the official MongoDB software.
- Start the MongoDB process
- Perform CRUD Operations with MongoDB
- Operations to create
- Read operations
- Operations to Update
- Delete Operations
Let's learn how we can install MongoDB if we are on MacOS; For this we will assume that you have Homebrew installed, which is simply a package manager for MacOS and Linux.
With our package manager, nothing could be easier, the first thing we have to do is add the MongoDB repository to our package manager.
Software required to install install MongoDB
Install Command line tools for xcode
Surely when you go to execute the Brew command to install the package, it will ask you to install the command line tools for xcode, accept and download and install these tools.
Install Homebrew
Now yes, we are going to install Homebrew, MacOS does not include the Homebrew preparation package by default; Therefore, you have to install it as indicated on the official website. https://brew.sh/#install
Homebrew installs the things you need for your MacOS from a terminal easily.
Install MongoDB Homebrew Tap
Issue the following from the terminal to tap the official MongoDB Homebrew tap: https://github.com/mongodb/homebrew-brew
This is a custom Homebrew Tap (package) for the official MongoDB software.
brew tap mongodb/brew
brew install mongodb-community@5.0https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/
Start the MongoDB process
With this we now have MongoDB on our team; the next thing we are going to do is start the process, since if we execute in our terminal:
brew services start mongodb-communityBecause if you don't start it and type mongo in the terminal, you will see an error like the following
It is true that an error like the following occurs:
MongoDB shell version v5.0.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:372:17Perform CRUD Operations with MongoDB

We are going to know how we can perform CRUD type operations using MongoDB, remember that CRUD type operations are those that allow us to Create, Read, Update and Delete (CRUD) respectively.
Operations to create
Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection.
MongoDB provides the following methods to insert documents into a collection:
db.collection.insertOne()
db.collection.insertMany()Example operations:
var user2 = {
name: 'Andres',
last_name: 'Cruz',
age: 29,
email: 'andres@gmail.com'
}
var user3 = {
name: 'Pablo',
last_name: 'Lama',
age: 30,
email: 'pablo@gmail.com'
}
var user4 = {
name: 'Luis',
last_name: 'Yello',
age: 99,
email: 'pepe@gmail.com'
}
db.users.insertOne(user2)
db.users.insertMany(
[user3, user4, user2]
)Which allow inserting a single record or more than one respectively.
Read operations
Read operations retrieve documents from a collection; that is, consult a collection of documents. MongoDB provides the following operation to read documents from a collection:
db.collection.find()
Operaciones de ejemplo:
db.users.find(
{ age: 25 },
{ name: true, email: true, _id: false }
).pretty()
db.users.find(
{ age: 25 },
{ email: false, _id: false }
).pretty()Operations to Update
Update operations modify existing documents in a collection. MongoDB provides the following methods to update documents in a collection:
db.collection.updateOne()
db.collection.updateMany()
db.collection.replaceOne()
Operaciones de ejemplo:
db.users.updateMany(
{
name: {
$exists: true
}
},
{
$set: {
name2: "Luisito"
}
}
)
db.users.updateOne(
{
name: {
$exists: true
}
},
{
$unset: {
name2: "Luisito"
}
}
)Delete Operations
Delete operations remove documents from a collection. MongoDB provides the following methods to remove documents from a collection:
db.collection.deleteOne()
db.collection.deleteMany()Example operations:
db.users.deleteOne({"_id": new mongo.ObjectID(id) })
db.users.deleteMany({"_id": new mongo.ObjectID(id) })
I agree to receive announcements of interest about this Blog.
Mongo is a NoSQL database of the moment, on MacOS you can have it EASY, and Let's learn how we can perform CRUD type operations using MongoDB from its command line.