To check all databases:-
MongoDB Enterprise > show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
MongoDB Enterprise >
To check the DB Stats :
> db.stats()
{
"db" : "db2",
"collections" : 3,
"objects" : 4,
"avgObjSize" : 47,
"dataSize" : 188,
"storageSize" : 16384,
"numExtents" : 3,
"indexes" : 1,
"indexSize" : 8176,
"fileSize" : 67108864,
"nsSizeMB" : 16,
"dataFileVersion" : {
"major" : 4,
"minor" : 5
},
"ok" : 1
}
>
>
To create a capped collection:
> db.createCollection("mycoll", {capped: true, size:100000});
{ "ok" : 1 }
To check the collection is capped or not:
> db.mycoll.isCapped()
true
>
To Convert a Collection to Capped Collection
> db.createCollection("doccollect4")
{ "ok" : 1 }
> db.doccollect4.isCapped()
false
>
> db.runCommand({"convertToCapped":"doccollect4",size:200000,max:10000});
{ "ok" : 1 }
To check all collections
> show collections
TEST
doccollect4
mycoll
system.indexes
how to insert a document to the collection :
MongoDB Enterprise > db.doccollect.insert({"title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "Vimal", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 });
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.doccollect.insert({"title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 });
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.doccollect.insert({"title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 });
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.doccollect.insert({"title" : "casandra doc1", "description" : "NoSQLDB", "by" : "Ashok", "tags" : [ "NoSQL", "DocDB", "NotOnlySQL" ], "likes" : 100 });
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise >
To find the document from the doccollect :-
MongoDB Enterprise > db.doccollect.find();
{ "_id" : ObjectId("5b7e43714b924fa09c96849d"), "title" : "MongoDB doc1", "description" : "Mongodb Book", "by" : "Ashok", "tags" : [ "NoSQL" ], "likes" : 100 }
{ "_id" : ObjectId("5b7e44764b924fa09c96849e"), "title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "Vimal", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b7e44aa4b924fa09c96849f"), "title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b7e44aa4b924fa09c9684a0"), "title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b7e44aa4b924fa09c9684a1"), "title" : "casandra doc1", "description" : "NoSQLDB", "by" : "Ashok", "tags" : [ "NoSQL", "DocDB", "NotOnlySQL" ], "likes" : 100 }
MongoDB Enterprise >
MongoDB Enterprise > show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
MongoDB Enterprise >
Connect to specific DB :-
MongoDB Enterprise > use MongoDB
switched to db MongoDB
To check the db connection:-
MongoDB Enterprise > db
MongoDB
MongoDB Enterprise >
To check the DB Stats :
> db.stats()
{
"db" : "db2",
"collections" : 3,
"objects" : 4,
"avgObjSize" : 47,
"dataSize" : 188,
"storageSize" : 16384,
"numExtents" : 3,
"indexes" : 1,
"indexSize" : 8176,
"fileSize" : 67108864,
"nsSizeMB" : 16,
"dataFileVersion" : {
"major" : 4,
"minor" : 5
},
"ok" : 1
}
>
>
To create a capped collection:
> db.createCollection("mycoll", {capped: true, size:100000});
{ "ok" : 1 }
To check the collection is capped or not:
> db.mycoll.isCapped()
true
>
To Convert a Collection to Capped Collection
> db.createCollection("doccollect4")
{ "ok" : 1 }
> db.doccollect4.isCapped()
false
>
> db.runCommand({"convertToCapped":"doccollect4",size:200000,max:10000});
{ "ok" : 1 }
To check all collections
> show collections
TEST
doccollect4
mycoll
system.indexes
how to insert a document to the collection :
MongoDB Enterprise > db.doccollect.insert({"title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "Vimal", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 });
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.doccollect.insert({"title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 });
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.doccollect.insert({"title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 });
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.doccollect.insert({"title" : "casandra doc1", "description" : "NoSQLDB", "by" : "Ashok", "tags" : [ "NoSQL", "DocDB", "NotOnlySQL" ], "likes" : 100 });
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise >
To find the document from the doccollect :-
MongoDB Enterprise > db.doccollect.find();
{ "_id" : ObjectId("5b7e43714b924fa09c96849d"), "title" : "MongoDB doc1", "description" : "Mongodb Book", "by" : "Ashok", "tags" : [ "NoSQL" ], "likes" : 100 }
{ "_id" : ObjectId("5b7e44764b924fa09c96849e"), "title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "Vimal", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b7e44aa4b924fa09c96849f"), "title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b7e44aa4b924fa09c9684a0"), "title" : "MongoDB doc1", "description" : "MongoDB document 1", "by" : "ashok", "tags" : [ "mongodb", "NoSQLDB", "DocDB", "database" ], "likes" : 100 }
{ "_id" : ObjectId("5b7e44aa4b924fa09c9684a1"), "title" : "casandra doc1", "description" : "NoSQLDB", "by" : "Ashok", "tags" : [ "NoSQL", "DocDB", "NotOnlySQL" ], "likes" : 100 }
MongoDB Enterprise >
To View the document in a formatted way by using pretty() command:
MongoDB Enterprise > db.doccollect.find().pretty();
{
"_id" : ObjectId("5b7e43714b924fa09c96849d"),
"title" : "MongoDB doc1",
"description" : "Mongodb Book",
"by" : "Ashok",
"tags" : [
"NoSQL"
],
"likes" : 100
}
{
"_id" : ObjectId("5b7e44764b924fa09c96849e"),
"title" : "MongoDB doc1",
"description" : "MongoDB document 1",
"by" : "Vimal",
"tags" : [
"mongodb",
"NoSQLDB",
"DocDB",
"database"
],
"likes" : 100
}
{
"_id" : ObjectId("5b7e44aa4b924fa09c96849f"),
"title" : "MongoDB doc1",
"description" : "MongoDB document 1",
"by" : "ashok",
"tags" : [
"mongodb",
"NoSQLDB",
"DocDB",
"database"
],
"likes" : 100
}
{
"_id" : ObjectId("5b7e44aa4b924fa09c9684a0"),
"title" : "MongoDB doc1",
"description" : "MongoDB document 1",
"by" : "ashok",
"tags" : [
"mongodb",
"NoSQLDB",
"DocDB",
"database"
],
"likes" : 100
}
{
"_id" : ObjectId("5b7e44aa4b924fa09c9684a1"),
"title" : "casandra doc1",
"description" : "NoSQLDB",
"by" : "Ashok",
"tags" : [
"NoSQL",
"DocDB",
"NotOnlySQL"
],
"likes" : 100
}
MongoDB Enterprise >
MongoDB Enterprise > db.doccollect.find().pretty();
{
"_id" : ObjectId("5b7e43714b924fa09c96849d"),
"title" : "MongoDB doc1",
"description" : "Mongodb Book",
"by" : "Ashok",
"tags" : [
"NoSQL"
],
"likes" : 100
}
{
"_id" : ObjectId("5b7e44764b924fa09c96849e"),
"title" : "MongoDB doc1",
"description" : "MongoDB document 1",
"by" : "Vimal",
"tags" : [
"mongodb",
"NoSQLDB",
"DocDB",
"database"
],
"likes" : 100
}
{
"_id" : ObjectId("5b7e44aa4b924fa09c96849f"),
"title" : "MongoDB doc1",
"description" : "MongoDB document 1",
"by" : "ashok",
"tags" : [
"mongodb",
"NoSQLDB",
"DocDB",
"database"
],
"likes" : 100
}
{
"_id" : ObjectId("5b7e44aa4b924fa09c9684a0"),
"title" : "MongoDB doc1",
"description" : "MongoDB document 1",
"by" : "ashok",
"tags" : [
"mongodb",
"NoSQLDB",
"DocDB",
"database"
],
"likes" : 100
}
{
"_id" : ObjectId("5b7e44aa4b924fa09c9684a1"),
"title" : "casandra doc1",
"description" : "NoSQLDB",
"by" : "Ashok",
"tags" : [
"NoSQL",
"DocDB",
"NotOnlySQL"
],
"likes" : 100
}
MongoDB Enterprise >
- TO SHUTDOWN MONGODB server (mongod core processes)
###############################################################################
> db.shutdownServer();
shutdown command only works
with the admin database; try 'use admin'
>
>
> use admin
switched to db admin
>
>
> db.shutdownServer();
2018-07-17T10:42:33.547+0700
DBClientCursor::init call() failed
server should be down...
2018-07-17T10:42:33.550+0700
trying reconnect to 127.0.0.1:27017
2018-07-17T10:42:33.550+0700
reconnect 127.0.0.1:27017 failed couldn't connect to server 127.0.0.1:27017
>
>
> exit
bye
[dsmongousr@localhost db]$
mongo
MongoDB shell version: 2.5.2
connecting to: test
2018-07-17T10:42:58.802+0700
Error: couldn't connect to server 127.0.0.1:27017 at
src/mongo/shell/mongo.js:145
exception: connect failed
[dsmongousr@localhost db]$
--
##############################################################################################################################################################
-TO START MONGODB server
(mongod core processes) ###############################################################################
[dsmongousr@localhost ~]$
whoami
dsmongousr
[dsmongousr@localhost ~]$ .
.bash_profile
[dsmongousr@localhost ~]$
[dsmongousr@localhost ~]$ cd
$MONGO_HOME
[dsmongousr@localhost ~]$ cat
start.sh
nohup mongod --dbpath /data/db
--fork --logpath /data/log/mongo.log
[dsmongousr@localhost ~]$ nohup
mongod --dbpath /data/db --fork --logpath /data/log/mongo.log
nohup: appending output to
`nohup.out''
[dsmongousr@localhost
~]$ tail -200f nohup.out
about to fork child process,
waiting until server is ready for connections.
forked process: 2848
child process started
successfully, parent exiting
about to fork child process,
waiting until server is ready for connections.
forked process: 2863
ERROR: child process failed,
exited with error number 100
about to fork child process,
waiting until server is ready for connections.
forked process: 3001
child process started
successfully, parent exiting
[dsmongousr@localhost ~]$ mongo
MongoDB shell version: 2.5.2
connecting to: test
Server has startup warnings:
2018-07-17T10:44:55.089+0700
[initandlisten]
2018-07-17T10:44:55.089+0700
[initandlisten] ** NOTE: This is a development version (2.5.2) of MongoDB.
2018-07-17T10:44:55.089+0700
[initandlisten] ** Not recommended for production.
2018-07-17T10:44:55.089+0700
[initandlisten]
>
> show dbs
db2 0.078GB
local 0.078GB
ashokdb 0.078GB
>
> use ashokdb
switched to db ashokdb
> show collections
doccollect
system.indexes
users10
>
--##############################################################################################################################