How to create a collection in the MongoDb Database.

MongoDB Overview


We can say that MongoDb Database works with the concept of collections of multi-platform documents of high availability, performance and easy scalability, different from the traditional Databases that follow the Relational Model. Some authors classify it as a category of document-oriented databases, with a unique ID that identifies each document, all values are recorded in the form of documents, being able to consult the content elements of these documents.


Download our MongoDB scripts now to streamline your process and administration, you will be helping our work Visit here ...


MongoDB Structure


Collection is a group that can store several documents, making an analogy can be considered as if it were a Table in Relational Databases.


Documents is a group of value pairs that has the syntax required by JSON, without worrying about the data structure itself, they don't need to have the same set of fields or structure.


Database Comparisons:


Relational Database MongoDB

Database Database

Table Collection

Lines Documents


There are two ways you can create a MongoDB Collection:

  • You can create a collection instantly by inserting a document using the insert () method;

  • You can create it explicitly, using the createCollection () method. This method allows us to create a collection without inserting a document.


Creating a Mongodb Collection


In the MongoDB documentation are the following features of the script below:


db.createCollection( <name>,
   {
     capped: <boolean>,
     autoIndexId: <boolean>,
     size: <number>,
     max: <number>,
     storageEngine: <document>,
     validator: <document>,
     validationLevel: <string>,
     validationAction: <string>,
     indexOptionDefaults: <document>,
     viewOn: <string>,              // Added in MongoDB 3.4
     pipeline: <pipeline>,          // Added in MongoDB 3.4
     collation: <document>,         // Added in MongoDB 3.4
     writeConcern: <document>
   }
)

Let's understand each parameter

  • name: The name of the collection to create type string;

  • capped: When set to true, creates a capped collection,;

  • autoIndexId: Specify false to disable the automatic creation of an index on the _id field;

  • size: Maximum size in bytes for a capped collection;

  • max: Maximum number of documents allowed in the capped collection. Note that the size: field takes precedence over the max field;

  • storageEngine: Optional. Available for the WiredTiger storage engine only;

  • validator: Optional. Allows users to specify validation rules or expressions for the collection;

  • validationLevel: Optional. Determines how strictly MongoDB applies the validation rules to existing documents during an update;

  • validationAction: Optional. Determines whether to error on invalid documents or just warn about the violations but allow invalid documents to be inserted;

  • indexOptionDefaults: Optional. Allows users to specify a default configuration for indexes when creating a collection.

  • viewOn: The name of the source collection or view from which to create the view;

  • pipeline: An array that consists of the aggregation pipeline stage(s);

  • collation: Specifies the default collation for the collection;

  • writeConcern: Optional. A document that expresses the write concern for the operation.

Want to know more information visit docs.mongodb


Create a collection called "business ":

db.createCollection("business", { capped : true, size : 4500500, max : 4000 } )

Drop Collection


In MongoDB we use the command db.collection.drop () to drop a collection (Collection)

Now drop the collection with the name mycollection.

>use mydb
switched to db mydb
>show collections
business
mycollection
system.indexes
>db.mycollection.drop()true>

Again check the list of collections into database.

>show collections
business
system.indexes

At this point we have successfully completed another procedure.


Download our MongoDB scripts now to streamline your process and administration, you will be helping our work Visit here ...


67 views