Friday, 4 June 2021

Mongo shell: Connect to MongoDB server running on specific port

Using –port option, you can connect to MongoDB server running on specific port.

 

Example

mongo --port 8888

 

Previous                                                    Next                                                    Home

Run MongoDB server on specific port

While starting the MongoDB server, you can explicitly specify the port using --port option.

 

Example

mongod --port 8888 --dbpath /Users/Shared/data/db

Previous                                                    Next                                                    Home

MongoDB: Databases, collections, documents

A MongoDB server can host one or more databases. Each database can have one or more collections. A collection can store documents.


If you are from SQL world, collection is like a table and document is like a row.



Previous                                                    Next                                                    Home

Mongo shell: Pretty print the output

‘pretty’ method prints the output in pretty print way.

 

Normal printing

> db.employee.find()
{ "_id" : ObjectId("60ba0a678e67ad0ff5b02998"), "id" : 1, "name" : "Krishna", "age" : 32, "designation" : "Architect" }
{ "_id" : ObjectId("60ba0b278e67ad0ff5b02999"), "id" : 34, "name" : "Anand", "age" : 42, "designation" : "Manager" }

 

Pretty printing

> db.employee.find().pretty()
{
	"_id" : ObjectId("60ba0a678e67ad0ff5b02998"),
	"id" : 1,
	"name" : "Krishna",
	"age" : 32,
	"designation" : "Architect"
}
{
	"_id" : ObjectId("60ba0b278e67ad0ff5b02999"),
	"id" : 34,
	"name" : "Anand",
	"age" : 42,
	"designation" : "Manager"
}

 

 

 

 

Previous                                                    Next                                                    Home

Mongo shell: fetch all the documents from collection

‘find’ method is used to fetch all the documents from the collection.

> db.employee.find()
{ "_id" : ObjectId("60ba0a678e67ad0ff5b02998"), "id" : 1, "name" : "Krishna", "age" : 32, "designation" : "Architect" }
{ "_id" : ObjectId("60ba0b278e67ad0ff5b02999"), "id" : 34, "name" : "Anand", "age" : 42, "designation" : "Manager" }

 

 

 

 

Previous                                                    Next                                                    Home

Mongo shell: Insert one document

‘insertOne’ method is used to insert one document to the collection.

 

Syntax

db.collection.insertOne(
   <document>,
   {
      writeConcern: <document>
   }
)

> db.employee.insertOne({"id" : 1, "name": "Krishna", "age" : 32, "designation" : "Architect"})
{
	"acknowledged" : true,
	"insertedId" : ObjectId("60ba0a678e67ad0ff5b02998")
}

 

When you insert a document into MongoDB, it automatically assigns a unique id to it. The type of this id is ObjectId. Document id is used to identify the document uniquely within the collection.

 

You can get all the documents from the collection using find method.

> db.employee.find()
{ "_id" : ObjectId("60ba0a678e67ad0ff5b02998"), "id" : 1, "name" : "Krishna", "age" : 32, "designation" : "Architect" }

 

 


 

Previous                                                    Next                                                    Home

Mongo shell: Show all the databases

‘show databases’ command print all the databases.

> show databases;
admin   0.000GB
config  0.000GB
local   0.000GB

 

‘admin’, ‘config’ and ‘local’ databases are for internal use and store mongoDB metadata.

 

 

Previous                                                    Next                                                    Home