Mongodb Commands for Beginners

Posted by Chaitanya Shahare on Fri, Apr 7, 2023

reference

what is MongoDb?

  • production ready database

  • document based database

difference between mongo and mongod

  • mongo : command line shell
  • mongod : mongo daemon, hosts the process

comparison of mongodb with mysql

  • sql equivalent to tables in excel
sql mongodb
database database
tables collections
rows documents (BSON)
columns fields

install mongodb

  • mongodb community server
  • mongodb compass

capped collection -> limiting the number of documents

  • bson has more types than json

basic commands

Database commands

  • View all databases show dbs;
  • Make a new database or switch to a database use comments
  • Show which database db
  • Delete database db.dropDatabase()

Collection commands

  • Show all collections show collections
  • Make a new collection named comments db.createCollection('myComments')
  • Delete a collection db.<name_of_collection>.drop()

Rows columns

  • Show all rows in the collection db.<name_of_collection>.find()

  • Find first row matching the object db.<name_of_collection>.find({name: "Chaitanya"})

  • Show all rows in the collection (prettified) db.<name_of_collection>.find().pretty()

  • Search for rows db.<name_of_collection>.find({lang: 'Python'})

  • Limit find results db.<name_of_collection>.find().limit(2)

  • Insert one Row db.<name_of_collection>.insert({ 'name': 'Chaitanya', 'lang': 'JavaScript', 'member_since': 2018 })

  • Insert many Rows `db.<name_of_collection>.insertMany([ { ’name’: ‘Chaitanya’, ’lang’: ‘JavaScript’, ‘member_since’: 2018 }, { ’name’: ‘Shahare’, ’lang’: ‘Python’, ‘member_since’: 2018 }, { ’name’: ‘Chaitanya’, ’lang’: ‘JavaScript’, ‘member_since’: 2018 },

    ])`

  • Delete Row db.<name_of_collection>.remove({name: "shahare"})

  • Count the number of rows in the output db.<name_of_collection>.find().count()

  • Sorting db.<name_of_collection>.find().sort({member_since: -1}).pretty()

  • Update a rows db.<name_of_collection>.update({name: 'Chaitanya'}, { 'name': 'Chaitanya', 'lang': 'C++', 'member_since': 2009 })

  • If you want to make a new object, if no matches are found, add a option db.<name_of_collection>.update({name: 'Chaitanya'}, { 'name': 'Chaitanya', 'lang': 'C++', 'member_since': 2009 }, {upsert: true})

Update operators

  • Increment update operator $inc db.<name_of_collection>.update({name: 'shahare'}, {$inc: { member_since: 2 }})
  • Rename update operator $rename db.<name_of_collection>.update({name: 'shahare'}, {$rename: { member_since: 2 }})

Filters

  • Less than $lt db.<name_of_collection>.find({ member_since: {$lt: 90} })
  • Less than equal to $lte db.<name_of_collection>.find({ member_since: {$lte: 90} })
  • Greater than $gt db.<name_of_collection>.find({ member_since: {$gt: 90} })
  • Greater than equal to $gte db.<name_of_collection>.find({ member_since: {$gte: 90} })

MongoDb atlas

  • MongoDb on cloud
  • Organisation > Project > Cluster
  • Database user
  • Connect via react/compass
  • Add a connection IP
  • Connection string