MongoDB for Beginners
Tags: mongodb, nosql, beginners, cloud
Published: 2022-12-29 05:29:34

Hello Friends, 
In this post, I will show how to use Basic Commands of MongoDB. 

## What is MongoDB ?
MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License which is deemed non-free by several distributions.

## What is MongoDB Compass
MongoDB Compass is a powerful GUI for querying, aggregating, and analyzing your MongoDB data in a visual environment. Compass is free to use and source available, and can be run on macOS, Windows, and Linux.

![MongoDB Compass Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfv8a5vol8iy75gxoipw.png)

We will use the following commands in MongoDB Compass.

### Create database
```
use courses
```

### Show all databases
```
show dbs
```
### Delete the database
```
db.dropDatabase()
```

### Show all the collections in current db
```
show collections
```

### Create a new collection
```
db.createCollection("<collection_name>")
```

### Delete a collection
```
db.courses.drop()
```

### Insert A Row in collection
```
db.courses.insertOne({"course": "MCA", "college": "MBM"})
```

### Insert many rows in collection (table)
```
db.courses.insertMany([{"course": "BSC", "college": "SBK"}, {"course": "BCA", "college": "JNVU"}])
```

### Get all the rows from collection
```
db.courses.find()
```

### Searching specific rows in collection
```
db.courses.find({"courses": "MCA"})
```

### Get the number of rows in collection
```
db.courses.find().size()
```

### Get limited records from collection
```
db.courses.find().limit(2)
```

### Ascending / Descending sorting
```
db.courses.find().sort({"course": 1})   // Ascending order
db.courses.find().sort({"course": -1})  // Descending order
```

### Get only one record
```
db.courses.findOne({"course": "MCA"})
```

### Update only one record in collection
```
db.courses.updateOne({"course": "MCA"}, {$set: {"course": "MBA"}})
```

### Update Multiple records in collection
```
db.courses.updateMany({"course": "MCA"}, {$set: {"course": "MBA", "college": "JNVU"}})
```

### Delete a record
```
db.courses.deleteOne({"course": "MCA"})
```

### Delete multiple records
```
db.courses.deleteMany({"course": "MCA", "course": "MBM"})
```
| Commands | Explanation |
| --------- |:---------:| 
| use courses | Create a Database | 
| show dbs | Show all database | 
| db.dropDatabase() | Delete Database | 
| show collections | Show the collections in DB | 
| db.createCollection("<collection_name>") | Create a collection | 
| db.courses.drop() | Delete a collection |
| db.courses.insertOne({"course": "MCA", "college": "MBM"}) | Insert a row in collection |
| db.courses.insertMany([{"course": "BSC", "college": "SBK"}, {"course": "BCA", "college": "JNVU"}]) | Insert many rows | 
| db.courses.find() | Fetch all rows | 
| db.courses.find({"courses": "MCA"}) | Fetch specific row | 
| db.courses.find().size() | Number of rows in the collection | 
| db.courses.find().limit(2) | Fetch Limited rows | 
| db.courses.find().sort({"course": 1})  | Ascending order |
| db.courses.find().sort({"course": -1})  | Descending order |
| db.courses.findOne({"course": "MCA"}) | Get only one record |
| db.courses.updateOne({"course": "MCA"}, {$set: {"course": "MBA"}}) | Update only one record in collection |
| db.courses.updateMany({"course": "MCA"}, {$set: {"course": "MBA", "college": "JNVU"}}) | Update multiple records in collection | 
| db.courses.deleteOne({"course": "MCA"}) | Delete a specific record | 
| db.courses.deleteMany({"course": "MCA", "course": "MBM"}) | Delete multiple records |