"MongoDB Tutorial Lesson Four" MongoDB Create a collection

Source: Internet
Author: User
Tags mongodb tutorial

Reatecollection () method

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

Grammar:

The basic createcollection () command syntax is as follows:

DB.  CreateCollection(name, options)      

In the commands, name is the name of the collection to be created. Options is a file that specifies a collection of configurations

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.

Example:

The basic syntax for the CreateCollection () method to not use options is as follows:

>use testswitched to db Test>db.  CreateCollection("mycollection"){"OK":1}>    

You can check the Create collection command by using show collections

>show Collectionsmycollectionsystem.  Indexes  

The following example shows several important options for the syntax of the CreateCollection () method:

>Db.CreateCollection("MyCol", { capped : true, Autoindexid : true, size : 6142800 , Max : 10000  } ) {< Span class= "PLN" >  "OK"  :  1 }>      

In MongoDB, you do not need to create a collection. When inserting some files MongoDB automatically creates the collection.

>db.  Yiibai.  Insert({"name":"Yiibai"})>show Collectionsmycolmycollectionsystem.  Indexesyiibai>              

"MongoDB Tutorial Lesson Four" MongoDB Create a collection

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.