This article mainly introduces MongoDB basic commands, prerequisites, your local already installed MONGO.
First, the use of BASIC commands
1. Create a database
Command: Use
Use database_name: The user creates the database, if it exists, uses the database, and no is created.
Example: Creating a Database MyDB
>use MyDB
>switched to DB MyPort
2. Querying the currently selected database
Command: DB
3. Querying the Database list
Command: Show DB
Note: To display the database, you need to insert it into at least one file
4. Deleting a database
Command: Db.dropdatabase ()
Note: Delete the database that is in use, or delete the default test database if there is no selection
5. Create a Collection
Command: Db.createcollection (name, options)
Parameters |
Type |
Describe |
Name |
String |
The name of the collection to create |
Options |
Document |
Optionally, specify the memory size and index options |
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. |
Example:
>db.createcollection ("mycollection")
{"OK": 1}
> db.createcollection ("MyCol", {capped:true, autoindexid: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.
Example:
Getting Started with MongoDB