MongoDB Cloud Server

Start Server

  • Click on Open MongoDB Page in IDE
  • Create Instance
  • Click MongoDB CLI
  • Check version
~$ mongosh mongodb://root:hGjWJ1sdReI6q3ajJ3gXXuAy@172.21.217.115:27017
Current Mongosh Log ID: 671677e0ac0fee924e964032
Connecting to:          mongodb://<credentials>@172.21.217.115:27017/?directConnection=true&appName=mongosh+2.3.1
Using MongoDB:          3.6.3
Using Mongosh:          2.3.1

For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
   The server generated these startup warnings when booting
   2024-10-21T14:45:50.755+0000: 
   2024-10-21T14:45:50.755+0000: ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
   2024-10-21T14:45:50.755+0000: **          See http://dochub.mongodb.org/core/prodnotes-filesystem
------

test> db.version()
3.6.3

# List all DBs
test> show dbs
admin   80.00 KiB
config  48.00 KiB
local   64.00 KiB

# Create or connect to DB training
test> use training
switched to db training

# Create a collection in DB training
training> db.createCollection("mycollection")
{ ok: 1 }

# List all collections
training> show collections
mycollection

Show All DBs

  • show dbs

Create New DB

  • Create a new DB named training
  • use training
  • If the DB already exists it will connect to it and start using it

Create Collection

  • Create a collection named mycollection inside the training DB
  • db.createCollection("mycollection")

Show All Collections

  • show collections

Insert Document

  • Let’s insert dome documents into the collection mycollection
  • db.mycollection.insert({"color":"white","example":"milk"})
  • The above command inserts the json document {“color”:”white”,”example”:”milk”} into the collection.
  • Let’s insert another one:
  • db.mycollection.insert({"color":"blue","example":"sky"})
  • Insert more as you see below in the code
training> db.mycollection.insert({"color":"white","example":"milk"})

# DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{
  acknowledged: true,
  insertedIds: { '0': ObjectId('67167ac0ac0fee924e964033') }
}

# Let's insert another one
training> db.mycollection.insert({"color":"blue","example":"sky"})
{
  acknowledged: true,
  insertedIds: { '0': ObjectId('67167b3eac0fee924e964034') }
}

# Let's insert three more
training> db.mycollection.insert({"color":"red","example":"wall"})
{
  acknowledged: true,
  insertedIds: { '0': ObjectId('67167bc4ac0fee924e964035') }
}
training> db.mycollection.insert({"color":"orange","example":"sun"})
{
  acknowledged: true,
  insertedIds: { '0': ObjectId('67167bd7ac0fee924e964036') }
}
training> db.mycollection.insert({"color":"green","example":"money"})
{
  acknowledged: true,
  insertedIds: { '0': ObjectId('67167c15ac0fee924e964037') }
}

# Now let's count all the documents
training> db.mycollection.countDocuments()
5

# Let's list all the documents in the collection
training> db.mycollection.find()
[
  {
    _id: ObjectId('67167ac0ac0fee924e964033'),
    color: 'white',
    example: 'milk'
  },
  {
    _id: ObjectId('67167b3eac0fee924e964034'),
    color: 'blue',
    example: 'sky'
  },
  {
    _id: ObjectId('67167bc4ac0fee924e964035'),
    color: 'red',
    example: 'wall'
  },
  {
    _id: ObjectId('67167bd7ac0fee924e964036'),
    color: 'orange',
    example: 'sun'
  },
  {
    _id: ObjectId('67167c15ac0fee924e964037'),
    color: 'green',
    example: 'money'
  }

# Disconnect from DB 
training> exit
~:/~/project$

Count All Document

  • Let’s count the number of documents in the collection
  • db.mycollection.countDocuments()

List All Documents

  • You can list/show all the documents using find()
  • db.mycollection.find()
  • Notice that MongoDB automatically add an Id field to every document

Exit

  • exit
  • To disconnect from the DB