How to Create,Show,Rename and Drop A Collection In MongoDB 4.0.11 on CentOS 7.6
Create,Show,Drop and Rename the Collection In MongoDB 4.0.11 On CentOs 7.6
Syntax :
db.createCollection(name)
show collections;
db.collectionname.renameCollection("New-Collection-Name")
db.collection_name.drop()
Process :
Check the version of MongoDB as follows
[root@linuxhelp ~]# mongod --version
db version v4.0.11
git version: 417d1a712e9f040d54beca8e4943edce218e9a8c
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64
Login to the Mongo Shell using the below command
[root@linuxhelp ~]# mongo
MongoDB shell version v4.0.11
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e13b3eec-2e21-4c0a-a4c2-0571a32d1b3e") }
MongoDB server version: 4.0.11
Server has startup warnings:
2019-08-06T15:33:04.607+0530 I CONTROL [initandlisten]
2019-08-06T15:33:04.607+0530 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-08-06T15:33:04.607+0530 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-08-06T15:33:04.607+0530 I CONTROL [initandlisten]
2019-08-06T15:33:04.607+0530 I CONTROL [initandlisten]
2019-08-06T15:33:04.607+0530 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-08-06T15:33:04.607+0530 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-08-06T15:33:04.607+0530 I CONTROL [initandlisten]
2019-08-06T15:33:04.607+0530 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-08-06T15:33:04.607+0530 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-08-06T15:33:04.607+0530 I CONTROL [initandlisten]
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
#### List the databases present by default in mongoDB
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
### Create a Database named test as follows
> use test;
switched to db test
### Insert an Users Document whose id is 1 as follows
> db.users.insert( { "id" : 1 } );
WriteResult({ "nInserted" : 1 })
### List the collections available in the current database as follows
> show collections;
users
#### List the databases available in the MongoDB
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
##### Create a Collection Using the following Syntax
> db.createCollection("Linuxhelp")
{ "ok" : 1 }
##### List the collection in the current databse using the following syntax
> show collections;
Linuxhelp
users
###### Rename the Collection “Users” as follows
> db.users.renameCollection("Linux");
{ "ok" : 1 }
####### List the collections in the current database test as follows to verify the renamed Collection.
> show collections;
Linux
Linuxhelp
##### Drop the Collection as follows
> db.Linuxhelp.drop();
true
###### List the collections in the test database to verify whether the collection has been dropped or not.
> show collections;
Linux
Q
What is the default port of MongoDB?
A
The default port of MongoDB is 27017
Q
Does MongoDB have access control enabled by default?
A
No ,MongoDB has no access control enabled by default.
Q
What does MongoDB databases hold?
A
In MongoDB,databases holds collections of documents.
Q
What is the Document Format that MongoDB stores in Collection?
A
The Document format that MongoDB stores In Collection Is BSON.
Q
What does Collection store In MongoDB?
A
In MongoDB , Collection Stores documents