Skip to content

Commit

Permalink
Improve lesson 1
Browse files Browse the repository at this point in the history
Signed-off-by: Ali Yousuf <aly.yousuf7@gmail.com>
  • Loading branch information
alyyousuf7 committed Oct 7, 2019
1 parent 481318b commit 02ad48c
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions lesson-1/README.md
@@ -1,9 +1,7 @@
# MongoDB Shell Basics
The goal is to get a basic idea of Mongo Shell commands and how to use them


### 1. Start mongo daemon

```
C:\> net start MongoDb // if installed as windows service
Expand All @@ -17,14 +15,13 @@ C:\> "C:\Program Files\MongoDB\Server\4.2\bin\mongo.exe"
```

### 2. Start mongo shell

```
C:\> mongo
```

Note: this will connect using the default port 27017. Use -p for specifying a different port.
Note: This will connect using the default port `27017`. Use `-p` for specifying a different port.

You should get an output similar to the following
You should get something similar to this:

```
MongoDB shell version v4.2.0
Expand All @@ -36,10 +33,8 @@ MongoDB server version: 4.2.0

From this moment on, everything you’ll type will be executed in the mongo shell.


### 3. List databases

Your server may host several different databases you have access to
Your server may host several databases you have access to.

```
> show dbs
Expand All @@ -48,15 +43,15 @@ config 0.000GB
local 0.000GB
```

### 4. Find the current database your are connection with
### 4. Find the database you're using
```
>db
> db
test
```

### 5. Select / Create a database

Database is automatically created if it does not exists
### 5. Switch / Create a database
To switch the database, you can run the `use` command.
If the database doesn't exist, it will be created for you.

```
> use shop
Expand All @@ -67,51 +62,47 @@ shop

Note: `show dbs` will not show the new database because it is empty.


### 6. Create collection


### 6. Create collection
```
db.createCollection("products")
> db.createCollection("products")
OR
db.products.insertOne({name:"paper",price:10, description:"A4 size papers"})
OR
> db.products.insertOne({name:"paper",price:10, description:"A4 size papers"})
```

Collections can be capped by setting size / max documents.

### 7. Run few insert commands to insert some data

```
db.products.insertOne({name:"Book",price:10, description:"A book"})
> db.products.insertOne({name:"Book",price:10, description:"A book"})
```

### 8. List collections
```
>show collections
> show collections
products
```

### 9. Find all documents

```
>db.products.find()
> db.products.find()
>db.products.find().pretty()
> db.products.find().pretty()
```

### 10. View logs

```
> show log global
```

### Some more commands
you can always use help command to get list of all available commands
You can always use `help` command to get list of all available commands.

To list database specific commands, you may use `db.help()`.

```
> db.dropDatebase()
> db.getState()
```
>db.dropDatebase()
>db.getState()
```

0 comments on commit 02ad48c

Please sign in to comment.