Detailed MongoDB How to create a collection and delete a collection in the _mongodb

Source: Internet
Author: User
Tags mongodb

Create a collection: CreateCollection () method

MongoDB db.createcollection (name, options)
is used to create a collection.

Grammar:
The basic createcollection () command syntax is as follows:

Db.createcollection (name, options)

In the command, name is the name of the collection that you want to create. Options is a file that specifies the set of configurations
Parameters Type Describe
Name String The name of the collection to create
Options Document (optional) Specify the memory size and indexing options

Option parameters are optional, so you only need to go to the specified collection name. The following are a list of options that you can use:
Field Type Describe
Capped Boolean (optional) If true, enable the CAP collection. A CAP collection is a fixed size collection that automatically overwrites the earliest entries when it reaches its maximum size. If you specify true, you need to also specify dimension parameters.
Autoindexid Boolean (optional) If true, the default value for automatically creating an indexed _id field is false.
Size Number Optionally, specify the maximum size byte cap collection. 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 you insert a document, MongoDB the first check Size field Cap collection, and then it checks the largest field.

Example:
The basic syntax for CreateCollection () methods that do not use options is as follows:

 >use test switched to DB Test >db.createcollection ("mycollection") {"OK": 1} >

You can check show collections by using the collection command you created
 >show Collections mycollection System.indexes

The following example shows the syntax for several important options createcollection () methods:

 >db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } ) { "ok" : 1 } >
In MongoDB, you do not need to create a collection. When you insert some files MongoDB a collection that is automatically created.

>db.yiibai.insert({"name" : "yiibai"}) >show collections mycol mycollection system.indexes yiibai >


Delete collection: Drop () method

MongoDB's

 Db.collection.drop () 
is used to delete a collection from the database.

Grammar:

The basic syntax for the drop () command is as follows

 Db. Collection_name.drop ()

Example:

First, check the available collections in the database MyDB

 >use MyDB switched to DB MyDB >show collections MyCol mycollection system.indexes yiibai >

Now delete the collection name mycollection

 >db.mycollection.drop () True >

Check the list of collections in the database again
 >show collections MyCol System.indexes Yiibai >

The drop () method returns True and returns False if the selected collection is discarded successfully
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.