Install on mac

brew tap mongodb/brew
brew install mongodb-community@4.0

Run

mongod --config /usr/local/etc/mongod.conf

Connect

mongo

You can connect to different host, using different port or connect directly to a database by using something like:

mongo 192.168.0.5:27018/shop

User name password

mongo --username USERNAME --password PASSWORD 127.0.0.1:27018/shop

Create database

Creating a database is as easy as tell mongodb to use it and inserting something in it. So let’s start with “using” database objects.

use objects

A command to list existing databases is the following:

show dbs

As you see there no any database. So let’s insert something in objects.

Insert and query data

db.shop.insertOne({ item: "phone", details: {manufacturer: "Samsung", model: "S10", price: "909"}})

Now you can see that there is a new database.

show dbs

The shop in above is the name of your table (called collections). You can change it for anything you want. You can see available collections by calling:

show collections

Queries: sql vs mongo

db.shop.find({item: "phone"})

or

db.shop.find({"item": "phone"})

This corresponds to

SELECT * 
FROM shop
WHERE item = 'phone'
db.shop.insertMany([
    { item: "phone", details: {manufacturer: "Apple", model: "8"}},
    { item: "phone", details: {manufacturer: "Apple", model: "Xs", price: "1159"}}
])
SELECT * 
FROM shop
db.shop.find({})
SELECT * 
FROM shop
WHERE details.manufacturer = "Apple"
db.shop.find({"details.manufacturer": "Apple"})