MongoDB Create Database
Use Database method:
There is no create database command in MongoDB. Actually, MongoDB do not provide any command to create database.
SQL: To create a database, table and insert values in the table manually.
MongoDB: To create a database manually because MongoDB will create it automatically when you save the value into the defined collection at first time.
If there is no existing database, the following command is used to create a new database.
Syntax:
use DATABASE_NAME
If the database already exists, it will return the existing database.
To check the currently selected database, use the command db:
>db
To check the database list, use the command show dbs:
>show dbs
Insert document it to database:
>db.movie.insert({“name”:“javatpoint”})
MongoDB Drop Database
The dropDatabase command is used to drop a database,deletes the associated data files.
Syntax:
Now check the list of databases:
MongoDB Create Collection
In MongoDB, db.createCollection(name, options) is used to create collection. But usually you don?t need to create collection. MongoDB creates collection automatically when you insert some documents.
Syntax:
db.createCollection(name, options)
Name: is a string type, specifies the name of the collection to be created.
Options: is a document type, specifies the memory size and indexing of the collection.
>db.createCollection(“SSSIT”)
To check the created collection, use the command “show collections”.
>show collections
How does MongoDB create collection automatically
MongoDB creates collections automatically when you insert some documents.
For example:
Insert a document named seomount into a collection named SSSIT. The operation will create the collection if the collection does not currently exist.
>db.SSSIT.insert({“name” : “seomount”})
>show collections
SSSIT
If you want to see the inserted document, use the find() command.
Syntax:
db.collection_name.find()
MongoDB Drop collection
It completely removes a collection from the database
db.COLLECTION_NAME.drop()
Now drop the collection with the name SSSIT: