Categories: NoSQL

MongoDB Commands & Concepts – Rookies Quick Reference

The article presents some of the basic concepts and commands which could prove useful for rookies starting with MongoDB.
Key Concepts

Simply speaking, MongoDB is a very popular NoSQL database with document-oriented storage. If you are a SQL developer and having challenges understanding document-oriented database, check out this page on mapping between SQL to MongoDB mapping. For a detailed introduction on MondoDB, check out this introduction page.

Following are some of the key terminologies:

  • Database
  • Collections
  • Document
  • Field
  • Primary key (_id)
  • index

JSON-styled Documents: The most important concept is document-oriented storage, and the documents are JSON-styled. Thus, one would require to learn JSON very well to do well with MongoDB as JSON-styled document is at the heart of it.

As like MySQL server and client, MongoDB also has a server and client. Following are the commands:

mongod: Start the server
mongo: Start the client

Document:

Following is a sample document in the collection, “users”:

{
    _id: ObjectId("509a8fb2f3f4658bd2f783a0"),
    firstname: "Chris",
    lastname: "Johnson",
    age: 25,
    email: "chris.johnson@gmail.com"
}

Collection:

Simply speaking, Collection is a set of documents.

 

Key Commands

Before I list down some, I must say that you may see a detailed query samples  in comparison with traditional SQL statements on this page.

  • show dbs: Shows all the databases
  • use <dbname>: Use the database; Create the database if not present in the database list
  • show collections: Shows all the collections in any database

CRUD: Create A Collection Explicity

db.createCollection( "<collectionname>" )

CRUD: Create A Document

db..insert( ): Create the collection if does not exist and insert the document OR, Insert the document in the existing collection having name as . Following is the sample command:

//Insert a new document in the collection users
db.users.insert(
    {
        firstname: "Bill",
        lastname: "Sherwood",
        age: 32,
        email: "bill1994@hotmail.com"
    }
)
// Insert multiple documents or array of documents
db.users.insert(
    [
    { firstname: "Bill", lastname: "Sherwood", age: 32,email: "bill1994@hotmail.com"},
    { firstname: "Chris", lastname: "Johnson", age: 28,email: "chris123@gmail.com"},
    ]
)

CRUD: Select Documents

//Shows all the documents within a collection. 
db.<collectionname>.find()

CRUD: Update Documents

//Updates multiple documents set firstname = "William" whose age = 35
db.users.update(
  { age: 35 },
  { $set: { firstname: "William" } ,
  { multi: true }
)

CRUD: Delete Document

// Removes the document with age=35
db.users.remove(
    { age: 35 }
)
// Removes all the document
db.users.remove( {} )

As mentioned earlier, if you want to quickly learn these commands with equivalent MySQL commands in the sight, do bookmark the following link: http://docs.mongodb.org/manual/reference/sql-comparison/

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

View Comments

Share
Published by
Ajitesh Kumar
Tags: mongodbnosql

Recent Posts

Feature Engineering in Machine Learning: Python Examples

Last updated: 3rd May, 2024 Have you ever wondered why some machine learning models perform…

6 hours ago

Feature Selection vs Feature Extraction: Machine Learning

Last updated: 2nd May, 2024 The success of machine learning models often depends on the…

1 day ago

Model Selection by Evaluating Bias & Variance: Example

When working on a machine learning project, one of the key challenges faced by data…

1 day ago

Bias-Variance Trade-off in Machine Learning: Examples

Last updated: 1st May, 2024 The bias-variance trade-off is a fundamental concept in machine learning…

2 days ago

Mean Squared Error vs Cross Entropy Loss Function

Last updated: 1st May, 2024 As a data scientist, understanding the nuances of various cost…

2 days ago

Cross Entropy Loss Explained with Python Examples

Last updated: 1st May, 2024 In this post, you will learn the concepts related to…

2 days ago