Introduction

MongoDB is a object based database.

 

 

References

https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-os-x/

 

 

Installation and setting up a database

Reference: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-os-x/

 

Notes:

  • Uses Homebrew

 

STEP: Install the Xcode command line tools

xcode-select --install

 

STEP: Install Brew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Reference: https://brew.sh/#install

 

STEP: Mongodb using Brew

brew tap mongodb/brew

brew update

brew install mongodb-community@6.0

 

The Mongodb community edition contains these binaries

• The mongodb server

• The mongos shared cluster query router

• The MongoDB shell, mongosh

 

STEP:  Check installation has worked 

brew --prefix

 

STEP: Run MongoDB Community Edition

// To start the mongodb server
brew services start mongodb-community@6.0

// To check it is running
ps aux | grep -v grep | grep mongod



// To run as a background process
mongod --config /usr/local/etc/mongod.conf --fork

// To check it has started ok
brew services list


// To stop the mongodb server
brew services stop mongodb-community@6.0

 

STEP: Connect to the MongoDB database using shell

mongosh

 

 

STEP: Test accessing the database

// Insert
db.inventory.insertOne(
   { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)


db.inventory.insertMany([
   { item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
   { item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
   { item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])


// Find
db.inventory.find( { item: "canvas" } )



// To find all the documents
db.inventory.find( {} )

db.inventory.find().pretty();


// Delete all
db.inventory.deleteMany({})


// Update a record
db.inventory.updateOne(
   { item: "paper" },
   {
     $set: { "size.uom": "cm", status: "P" },
     $currentDate: { lastModified: true }
   }
)


// Update many records
db.inventory.updateMany(
   { "qty": { $lt: 50 } },
   {
     $set: { "size.uom": "in", status: "P" },
     $currentDate: { lastModified: true }
   }
)

 

 

const Cat = mongoose.model('Cat', {name: String, age: Number})
const kitty = new Cat({ name: 'Pussy', age: 23})
kitty.save().then(() => console.log('meow'))

const kittens = Cat.find()
console.log(kittens)