![]() |
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term ‘NoSQL’ means ‘non-relational’. It means that MongoDB isn’t based on the table-like relational database structure but provides an altogether different mechanism for the storage and retrieval of data. This format of storage is called BSON ( similar to JSON format). In this article, we will learn about the collection in MongoDB. We will also learn about document storage, schema-less nature, namespace, dynamic creation, indexing, and CRUD operations on the collection. Collections in MongoDBThe database is used to store related information. SQL stores data in tables, while MongoDB stores information in collections. A database can have one or many collections, and each collection must have a unique name. Collections store information about objects or entities, such as cars. For example, a collection can store different models and their related features in the form of documents. Documents are similar to rows/records in SQL tables and store data in BSON format. A collection can have any number of documents, each with a unique ID and data. Features of collection
NamespaceNamespace is a combination of the database name and the collection name or view name with a dot as a separator. The maximum possible length of the namespace is 255 bytes .It is represented as “database-name. collection-name”. CRUD operationWe can perform the basic Create, Read, Update and Delete Operations on Collections in MongoDB. Create Collectionuse db_name;
Read From the Collectiondb.collection_name.find({})
find() method is used to along with empty {} are used to read all the documents in the collection Example: use Geeks; ![]() Collections in MongoDb In the above example ,Geeks database is used to store the student Collection.Two documents are inserted into the collection using the insertMany() method.The inserted documents are read using find() method. Update OperationTo update the documents we can use updateOne() or updateMany methods. Syntax: db.collection_name.update( query, update, optional)
Delete Documents from the CollectionThe delete operation are used to delete or remove the documents from a collection. You can perform delete operations using the deleteOne() method and deleteMany() method db.collection_name.deleteMany( condtion ); Example: Update the favSub field of rollNo 102; db.student.updateOne({ rollNo: 102 }, { $set: { favSub: "Cloud Computing" } });
![]() Update Collection Indexing on CollectionIndex is created on the collection to enhance the performance of the query i.e. fetching the data in a short span of time without iterating over the whole dataset. Indexes make searching in a collection easier with the limited number of documents. Binary tree data structure is used as an index. In documents, the _id field is a default index which is automatically created by MongoDB and we are not allowed to drop this index.. The indexes are order by the value of the field specified in the index. createIndex() method is used to create the index and getIndexes() method is used to fetch the indexes on collection. Syntax: db.<collection>.createIndex( { <field>: “text”} )
Example: db.student.createIndex({ rollNo: 1 }); ![]() Indexing in Collection In the above example we create a custom index on student collection using the createIndex() method.The createIndex method is passed with the rollNo field to create a index on it.When we use getIndexes() method it return 2 index , _id and rollNo as _id is the default index . |
Reffered: https://www.geeksforgeeks.org
Node.js |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |