MongoDB Getting started with the mongosh
kodingwindow@kw:~$ mongosh
...

test> db.version()
7.0.9

test> version()
2.2.5

test> db.runCommand({whatsmyuri:1}) { you: '127.0.0.1:57612', ok: 1 } test> db.getMongo() mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5
test> "Hello World" Hello World test> "Hello World".replace("World","Learners") Hello Learners
Arithmetic Operations
test> a=1000
1000

test> b=30
30

test> a+b
1030

test> a*b
30000

test> a-b
970

test> b-a
-970

test> (a+b)*a
1030000

test> a/b
33.333333333333336

test> a=1
1
test> b=0
0
test> a/b
Infinity
Date and Time Functions
test> Date()
Thu Sep 28 2023 15:15:51 GMT+0530 (India Standard Time)

test> new Date()
ISODate("2023-09-28T09:46:17.052Z")

test> new Date().getTime()
1695894392513

test> new Date().getTimezoneOffset()
-330
db.hostInfo() Function
test> db.hostInfo()
{
    system: {
      currentTime: ISODate("2023-09-28T09:46:58.600Z"),
      hostname: 'kw',
      cpuAddrSize: 64,
      memSizeMB: Long("7717"),
      memLimitMB: Long("7717"),
      numCores: 4,
      numPhysicalCores: 2,
      numCpuSockets: 1,
      cpuArch: 'x86_64',
      numaEnabled: false,
      numNumaNodes: 1
    },
    os: { type: 'Linux', name: 'Ubuntu', version: '23.04' },
    ...
}
Running Hosts and Services
kodingwindow@kw:~$ su
Password: 
root@kw:/home/kodingwindow# apt-get install nmap

kodingwindow@kw:~$ nmap -p- localhost
Starting Nmap 7.93 ( https://nmap.org ) at 2023-09-28 15:18 IST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0000020s latency).
Not shown: 65531 closed tcp ports (reset)
PORT      STATE SERVICE
631/tcp   open  ipp
3306/tcp  open  mysql
27017/tcp open  mongod
33060/tcp open  mysqlx

Nmap done: 1 IP address (1 host up) scanned in 0.43 seconds
db.isMaster Function
isMaster returns a document that describes the role of the mongod instance.
test> db.isMaster()
{
  ismaster: true,
  topologyVersion: {
    processId: ObjectId("6515445845ade65bfa7a393f"),
    counter: Long("0")
  },
  maxBsonObjectSize: 16777216,
  maxMessageSizeBytes: 48000000,
  maxWriteBatchSize: 100000,
  localTime: ISODate("2023-09-28T09:49:39.204Z"),
  logicalSessionTimeoutMinutes: 30,
  connectionId: 4,
  minWireVersion: 0,
  maxWireVersion: 21,
  readOnly: false,
  ok: 1,
  isWritablePrimary: true
}
db.stats() Function
test> db.stats()
{
  db: 'test',
  collections: Long("0"),
  views: Long("0"),
  objects: Long("0"),
  avgObjSize: 0,
  dataSize: 0,
  storageSize: 0,
  indexes: Long("0"),
  indexSize: 0,
  totalSize: 0,
  scaleFactor: Long("1"),
  fsUsedSize: 0,
  fsTotalSize: 0,
  ok: 1
}
Advertisement