Book Image

Mastering MongoDB 7.0 - Fourth Edition

By : Marko Aleksendrić, Arek Borucki, Leandro Domingues, Malak Abu Hammad, Elie Hannouch, Rajesh Nair, Rachelle Palmer
Book Image

Mastering MongoDB 7.0 - Fourth Edition

By: Marko Aleksendrić, Arek Borucki, Leandro Domingues, Malak Abu Hammad, Elie Hannouch, Rajesh Nair, Rachelle Palmer

Overview of this book

Mastering MongoDB 7.0 explores the latest version of MongoDB, an exceptional NoSQL database solution that aligns with the needs of modern web applications. This book starts with an informative overview of MongoDB’s architecture and developer tools, guiding you through the process of connecting to databases seamlessly. This MongoDB book explores advanced queries in detail, including aggregation pipelines and multi-document ACID transactions. It delves into the capabilities of the MongoDB Atlas developer data platform and the latest features, such as Atlas Vector Search, and their role in AI applications, enabling developers to build applications with the scalability and performance that today’s organizations need. It also covers the creation of resilient search functionality using MongoDB Atlas Search. Mastering MongoDB 7.0’s deep coverage of advanced techniques encompasses everything from role-based access control (RBAC) to user management, auditing practices, and encryption across data, network, and storage layers. By the end of this book, you’ll have developed the skills necessary to create efficient, secure, and high-performing applications using MongoDB. You’ll have the confidence to undertake complex queries, integrate robust applications, and ensure data security to overcome modern data challenges.
Table of Contents (20 chapters)
4
Chapter 4: Connecting to MongoDB

Index types

In the following section, you will explore different index types, understanding how and when to utilize them for optimal outcomes.

Single-field indexes and indexing embedded documents

The simplest and the most common type of index is the single-field index. By default, MongoDB automatically generates single-field indexes for every collection—a unique index on the _id field. This index prevents the insertion of a second document with the same value for the _id field into the collection.

In the previous section, you saw an example of the simplest possible single-field index. You can further enhance your books collection by adding a couple more indexes—one for the titles and one for the price, which is an embedded field, to make it unique.

Execute the following command:

db.books.createIndex({ "title": 1 }, { "unique": true })

Note

Two indexes cannot have the same shape even in cases when the options are different. For...