Mongodb Commands Cheat Sheet



In this post, you will learn about MongoDB commands which could get you started and perform minimum database related activities such as create, update, drop a collection (table). These commands are ideally meant for MongoDB beginners and could be taken as the cheat sheet. You may want to bookmark this page for quick reference.

MongoDB Commands Cheatsheet

The following is the list of the commands:

  1. Quick Access To Mongodb Commands Cheat Sheet 21 January,2019 by Jack Vamvas. MongoDB is an NoSQL document-oriented database program.
  2. This article is a cheat sheet for MongoDB commands. You will find a list of the most essential and handy commands that you will frequently use here. Introduction As a developer, you will need to frequent reference to MongoDB commands. Most importantly, you will frequently have to perform CRUD and filter operations. This cheat sheet.
    • Start and stop the MongoDB Database
    • Access the MongoDB database using Shell
    • Show all databases
    • Create a database, say, testdb; Switch to the database

      Until a collection is created in a database, the database name is not listed as a result of execution of the command, “show dbs”

    • Add a collection
    • Show all collections in a database; Execute the “use dbname” command to access the database before executing the command given below.

      The following command also work:

    • Insert a record in the collection; A record is inserted in the collection, “user”.
    • Display list of records of a collection; “user” collection is used.
    • Display a list of records matching with value (s) of specific fields
  • Drop the collection
  • Create users in the database; The below command creates a user with username as “ajitesh” and having the role such as “readWrite” and “dbAdmin”
  • Show users; If executed without selecting a database, it displays all users along with database information.
  • Login into the database with username and password

    For user created in above command, the login command would look like the following:

References

ADB (Android Debug Bridge) Commands General Maths Git Commands Markdown Shell Scripting Other Other Dbms Dbms Learning Path SQL Query Mongo db Mongo db MONGODB MongoDB Cheat Sheet MongoDB Cheat Sheet Table of contents Insert Row Insert Multiple Rows.

Mongodb Commands Cheat Sheet

Summary

In this post, you learned about the MongoDB cheatsheet commands (especially, helpful for beginners) which could help you quickly get started with MongoDB.

Commands

Did you find this article useful? Do you have any questions or suggestions about this article? Leave a comment and ask your questions and I shall do my best to address your queries.

  • First Principles Understanding based on Physics - April 13, 2021
  • Precision & Recall Explained using Covid-19 Example - April 11, 2021
  • Moving Average Method for Time-series forecasting - April 4, 2021

Introduction

If you’re looking to use a MongoDB database with your web application running on NodeJS you might be looking at which npm libraries which will allow you to easily interact with MongoDB. Mongoose and MongoJS are the two top choices for developers in this scenario. In this article we’ll give a quick cheat sheet of the common commands you’ll use with the Mongoose library that will quickly get you up and running. We’ll show basic commands and a very quick briefing on what each command does.

  • Notes:
    • To demo we will use the example of creating a database for a small grocery store with a grocery products as the records.
    • Most of these functions have several optional parameters including a callback that we are choosing not to show here for the sake of simplicity.

Cheat Sheet

Setup

To get setup with Mongoose you’ll have to have mongoose installed from npm using

Commands
npm install mongoose

Then you’ll need it required in your models and any js file where you are using it

Schema

The big difference between MongoJS and Mongoose is that Mongoose pushes you to create documents using a schema. You define a schema then create documents using that schema. With MongoJS on the other hand you’re free to do whatever you’d like. Some people prefer the freedom of MongoJS and some people prefer the structure of Mongoose. Since this is the major differentiating point we’ll start with creating a schema.

To create a schema your js will look something like this

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ProductSchema =new Schema({
name:{
type:String,
required:true
},
brand:{
type:String,
required:false
},
order:{
type: Schema.Types.ObjectId,
ref:'Order'
}
});
// This creates our model from the above schema, using mongoose's model method
var Product = mongoose.model('Product', ProductSchema);
// Export the Article model
module.exports= Product;

Mongodb Cheat Sheet Pdf

Commands

In this example we’ve created a simple schema that uses a couple primitive String fields “name” and “brand” but also an “order” field which could join with another Schema. This is like a Join in relational databases.

As you can see we create the schema, then create our model from that schema, then lastly we export that model so that our other javascript files can create documents using that model.

Creating documents based on the model

To use the model we need to import mongoose and the model. We have our model in a models folder so our code to do so looks like this

var mongoose = require('mongoose');
var db = require('./models');
mongoose.connect('mongodb://localhost:27017/grocerydb',{ useNewUrlParser:true});

Then we create a document like so

var product ={ name:'Soda', brand:'demoBrand'};
db.Product.create(product)
.then(function(dbProduct){
console.log(dbProduct);
})
.catch(function(err){
console.log(err);
});

We showed the .then() and .catch() for demonstration of how to handle success and failures but we will omit them in the future examples so we can solely concentrate on Mongoose commands.

Creating multiple documents `jsdb.Product.insertMany([{ name: ‘Soda’ }, { name: ‘Milk’}]);`

Finding documents

We can find all our documents (Products) with a command like below.

Find a document based on id

db.Product.findOne({ _id:<id>})

Updating

Update a single document

db.Product.updateOne({ name:'Soda'},{ brand:'newBrand'});

Update multiple documents

db.Product.updateMany({ brand:'demoBrand'},{ quantity:500});
Mongodb commands cheat sheet pdf

Mongodb Commands Cheat Sheet Pdf

Delete Documents

Delete a single document

Delete multiple documents

Mongodb Commands Cheat Sheet Pdf

db.Product.deleteMany({ quantity:{ $gte:100}});

Mongodb Commands Cheat Sheet Download

Conclusion

Mongodb Command Shell

In this cheat sheet we gave you the basic CRUD ( Create, Read, Update, Delete ) commands that will be commonly used with Mongoose. Mongoose is a very easy library to get up and running with so get started and try out some commands. You can install Mongoose from npm by running npm install mongoose. We hope this cheat serves as a good introduction or as a reminder of how to use Mongoose within your NodeJS. If you have any feedback on commands we could add please don’t hesitate to reach out to us.





Comments are closed.