Create a database
Use database_name is used to create a database. This command will create a new database if the database does not exist, or it will return the existing database.
Grammar:
Use database_name
To check the currently selected database using command DB
If you want to query the database list, use the command show DBS
The default database for MongoDB is test. If no database is created, the collection is saved in the test database.
Deleting a database
The MongoDB db.dropdatabase () command is used to delete an existing database.
Grammar:
Db.dropdatabase ()
eg. if you want to delete the new database <mydb>, the dropdatabase () command will look like this:
Create a Collection
MongoDB's db.createcollection (name, options) is used to create collections. In the command, name is the names of the collections to create. Options is a document that specifies the configuration of the collection
Parameters |
type |
Description |
Name |
String |
The name of the collection to create |
Options |
Document |
Optionally, specify the memory size and index options |
Options?? The parameter is optional, so only the specified collection name is required. The following is a list of options you can use:
Field |
type |
Description |
Capped |
Boolean |
(optional) If true, the CAP collection is enabled. A capped collection is a fixed-size collection that automatically overwrites the oldest entry when it reaches its maximum size. If True is specified, the dimension parameter needs to be specified as well. |
Autoindexid |
Boolean |
(optional) If true, the default value for the auto-create index _id field is false. |
Size |
Number |
Optionally, specify a maximum size byte capping set. If the cap is true, then you also need to specify this field. |
Max |
Number |
Optionally, specify the maximum number of files allowed in the capping collection. |
When inserting a document, the MongoDB first check Size field caps the collection, and then it checks the largest field.
The basic syntax for the createcollection () method to not use options is as follows:
You can check the Create collection command by using Show Collections
The following example shows several important options for the syntax of the CreateCollection () method:
true true, size:6142800, max:10000"OK": 1 }>
In MongoDB, you do not need to create a collection. When inserting some files MongoDB automatically creates the collection.
MongoDB Delete Collection
MongoDB related Operations