Using –port option, you can connect to MongoDB server running on specific port.
Example
mongo --port 8888
Previous Next Home
This blog is primarily focus on Java fundamentals and the libraries built on top of Java programming language. Most of the post are example oriented, hope you have fun in reading my blog....:)
Using –port option, you can connect to MongoDB server running on specific port.
Example
mongo --port 8888
Previous Next Home
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 HomeA 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.
‘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"
}
‘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" }
‘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" }
‘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.