Book Image

MongoDB 4 Quick Start Guide

By : Doug Bierer
Book Image

MongoDB 4 Quick Start Guide

By: Doug Bierer

Overview of this book

MongoDB has grown to become the de facto NoSQL database with millions of users, from small start-ups to Fortune 500 companies. It can solve problems that are considered difficult, if not impossible, for aging RDBMS technologies. Written for version 4 of MongoDB, this book is the easiest way to get started with MongoDB. You will start by getting a MongoDB installation up and running in a safe and secure manner. You will learn how to perform mission-critical create, read, update, and delete operations, and set up database security. You will also learn about advanced features of MongoDB such as the aggregation pipeline, replication, and sharding. You will learn how to build a simple web application that uses MongoDB to respond to AJAX queries, and see how to make use of the MongoDB programming language driver for PHP. The examples incorporate new features available in MongoDB version 4 where appropriate.
Table of Contents (11 chapters)

Using map-reduce

The db.collection.mapReduce() method delivers similar results to that of the aggregation pipeline. The main difference is that rather than performing operations in stages, map-reduce uses JavaScript functions to produce results. This gives you access to the full programming power that's available in JavaScript. Because it operates outside of the aggregation framework, however, performance is generally worse. If there is a high degree of complexity in your query, it might be worth considering using this feature. Otherwise, the MongoDB documentation recommends using the aggregation pipeline framework.

To demonstrate map-reduce functionality, we will use the same purchases collection that we described previously. Here is the general structure of a mapReduce() command:

When we run this method, we get operational metadata. Unlike the aggregate() method, the output...