0 0
Read Time:43 Second

MongoDB Collections

MongoDB Collections by CodeDixa

A Collection in MongoDB is similar to a table in RDBMS. MongoDB collections do not enforce schemas. Each MongoDB collection can have multiple documents. A document is equivalent to row in a table in RDBMS.

Create Collection

To create a collection, use the db.createCollection() command. The following creates a new employees collection in the current selected database.

create collection command mongodb codedixa
Create a Collection

Above, the employees collection is created using the creatCollection() method. It returns an object { ok: 1 }, which indicates the collection was created successfully.

As mentioned above, a single database can have multiple collections. The following creates multiple collections.

create multiple collections
Create Multiple Collection

Display all Collections

Use the show collections commands to list all the collections in a database.

Delete MongoDB Collection

To delete a collection, use the db.<collection-name>.drop() method.

delete MongoDB Collections
Delete MongoDB Collection
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Comment