Skip to main content

MongoDB - Create Collection

MongoDB - Create Collection



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

Syntax

Basic syntax of createCollection() command is as follows −

db.createCollection(name, options)


In the command, name is name of collection to be created. Options is a document and is used to specify configuration of collection.

Parameter

Type

Description

Name

String

Name of the collection to be created

Options

Document

(Optional) Specify options about memory size and indexing

Options parameter is optional, so you need to specify only the name of the collection.

Following is the list of options you can use −



Field

Type

Description

capped

Boolean

(Optional) If true, enables a capped collection. Capped collection is a fixed size collection that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also.

autoIndexId

Boolean

(Optional) If true, automatically create index on _id field.s Default value is false.

size

number

(Optional) Specifies a maximum size in bytes for a capped collection. If capped is true, then you need to specify this field also.

max

number

(Optional) Specifies the maximum number of documents allowed in the capped collection.


Examples

  • Basic syntax of createCollection() method without options is as follows −

>use school

switched to db school

>db.createCollection("mycollection")

{ "ok" : 1 }

>

  • You can check the created collection by using the command show collections.

>show collections

mycollection

system.indexes

  • The following example shows the syntax of createCollection() method with few important options −

>db.createCollection("mycol", { capped : true, autoIndexId : true, size : 

   6142800, max : 10000 } )

{ "ok" : 1 }


In MongoDB, you don't need to create a collection. MongoDB creates collection automatically, when you insert some document.

Capped collections are collections which can store data in the same order it is inserted.

  • you can't remove an individual object from the capped collection.

  • Capped collections can be used for logging, caching and auto-archiving.

  • Presently, the maximum size for a capped collection is 1e9(i.e. 1X109) for 32-bit machines. For 64 bit machines, there is no theoretical limit. Practically, it can be extended till your system resources permit.


Valid collection names

  • Collection names must begin with letters or an underscore.

  • A Collection name may contain numbers.

  • You can't use "$" character within the name of a collection. "$" is reserved.

  • A Collection name must not exceed 128 characters. It will be nice if you keep it within 80/90 characters.

  • Using a "." (dot) notation, collections can be organized in named groups. For example, tutorials.php and tutorials.javascript both belong to tutorials. This mechanism is called as collection namespace which is for user primarily. Databases don't have much to do with it.

In MongoDB, you don't need to create collection. MongoDB creates collection automatically, when you insert some document.

>db.codeforalgo.insert({"name" : "codeforalgo"})

>show collections

mycol

mycollection

codeforalgo





Comments

Popular posts from this blog

MongoDB: Data Types

MongoDB Data Types   : MongoDB stores documents on disk in the BSON serialization format. BSON is a binary representation of JSON documents, though BSON data format provides more data types than JSON.

MongoDB - Create Database

MongoDB - Create Database If you want to check your databases list, use the command show dbs Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it. MongoDB use DATABASE_NAME is used to create a database. > use database_name

Linear Regression (Python Implementation)

  Linear regression is perhaps one of the most well known and well-understood algorithms in statistics and machine learning. In this post, you will discover the linear regression algorithm, how it works, and how you can best use it in on your machine learning projects. Contents… •Linear Regression •Simple Linear Regression •Multiple Linear Regression •Assumptions •Applications L inear Regression A statistical approach for modeling relationship between a dependent variable with a given set of independent variables. We refer dependent variables as response and independent variables as  features Simple Linear Regression Simple linear regression is an approach for predicting a response using a single feature. It is assumed that the two variables are linearly related. Hence, we try to find a linear function that predicts the response value(y) as accurately as possible as a function of the feature or independent variable(x). Let us consider a dataset where we have a value of respon...