Collection Operations for MongoDB

Source: Internet
Author: User
Tags mongodb

MongoDB Create Collection

1. Manually Create:

Syntax format:

DB.  CreateCollection(name, options)      

Parameter description:

    • Name: The collection name to create
    • Options: Optional parameters, specifying option for memory size and index

Options can be the following parameters:

Field type Description
Capped Boolean (optional) If true, creates a fixed collection. A fixed collection is a set that has a fixed size that automatically overwrites the oldest document when it reaches its maximum value.
When this value is true, you must specify the size parameter.
Autoindexid Boolean (optional) If true, the index is automatically created in the _id field. The default is False.
Size Numerical Optionally, specify a maximum value (in bytes) for the fixed collection.
If capped is true, you also need to specify the field.
Max Numerical Optionally, specify the maximum number of documents contained in the fixed collection

For example: Create a Colle1 collection in the exam table:

> use examswitched to DB exam> Db.createcollection ("colle1""  OK"1  }>#查看当前数据库的集合colle1system.indexes

Create a fixed collection mycol, the entire collection space size 6142800 KB, the maximum number of documents is 10,000.

> db.createcollection ("mycol"truetrue, size:      614280010000"OK"1  }>

2. In MongoDB, you do not need to create a collection. When you insert some documents, MongoDB automatically creates the collection.

> show Collectionscolle1system.indexes> Db.exam.insert ({"name111""  rookie tutorial""ninserted"1  })> Show Collectionscolle1examsystem.indexes
MongoDB Delete Collection

Syntax format:

Db.collection.drop ()

return value

If the selected collection is successfully deleted, the drop () method returns True, otherwise false is returned.

For example: View all collections and delete one of the collections

> show collectionscolle1examsystem.indexes> show tablescolle1examsystem.indexes>  Db.colle1.drop ()true> show tablesexamsystem.indexes>

View a collection of current data can be used with show collections or show Tables

Collection Operations for MongoDB

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.