Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
MongoDB Fundamentals

You're reading from  MongoDB Fundamentals

Product type Book
Published in Dec 2020
Publisher Packt
ISBN-13 9781839210648
Pages 748 pages
Edition 1st Edition
Languages
Concepts
Authors (4):
Amit Phaltankar Amit Phaltankar
Profile icon Amit Phaltankar
Juned Ahsan Juned Ahsan
Profile icon Juned Ahsan
Michael Harrison Michael Harrison
Profile icon Michael Harrison
Liviu Nedov Liviu Nedov
Profile icon Liviu Nedov
View More author details

Table of Contents (15) Chapters

Preface
1. Introduction to MongoDB 2. Documents and Data Types 3. Servers and Clients 4. Querying Documents 5. Inserting, Updating, and Deleting Documents 6. Updating with Aggregation Pipelines and Arrays 7. Data Aggregation 8. Coding JavaScript in MongoDB 9. Performance 10. Replication 11. Backup and Restore in MongoDB 12. Data Visualization 13. MongoDB Case Study Appendix

6. Updating with Aggregation Pipelines and Arrays

Overview

This chapter introduces you to two additional features of update operations in MongoDB. You will first learn how to perform some complex update operations using pipeline support. Using pipeline support, you will be able to write a multi-step update expression and also refer to the values of other fields. Next, the chapter covers the updating of array fields in documents, which involves adding elements to an array, updating or deleting all or specific elements, creating arrays as a set, and sorting array elements. You will practice pushing unique elements to an array and sorting its elements as part of the final activity. By the end of this chapter, you will be able to derive update expressions based on the values of other fields and manipulate array fields in the documents of a collection.

Introduction

So far, we have covered querying using various operators to prepare query expressions. We have also learned how to create, delete, and modify documents in the collection, used various delete and update functions, and considered their differences and usability. We have also covered how to replace documents and how to perform upsert operations using a number of update operators. Now it is time to practice more complex update operations using the aggregation pipeline support, and learn how to modify arrays in a document.

We will begin this chapter with MongoDB pipeline support, where we will briefly introduce the aggregation pipeline and how it helps you to perform more complex update operations. We will then cover how to update array fields, how to add and sort elements of an existing array, and use an array as a set of unique elements. Next, you will learn how to remove the first, last, or another specific element from an array. Finally, you will learn how to prepare...

Updating Array Fields

In the previous sections, we learned about updating fields in one or more MongoDB documents. We also learned how to write update expressions using various operators and how to use MongoDB pipeline support. In this section, we will learn about updating array fields from a document.

To try some basic update operations on array fields, we will insert the following document into the movies collection:

db.movies.insert({"_id" : 111, "title" : "Macbeth"})

The document only has a title field and does not contain an array, so let's try creating one:

db.movies.findOneAndUpdate(
    {_id : 111},
    {$set : {"genre" : ["Unknown"]}},
    {"returnNewDocument" : true}
)

The preceding operation uses $set in the genre field. The value of genre is a single-element array—["unknown"]. The output can be seen here:

...

Summary

We started this chapter by learning how to update documents using aggregation pipeline support. Pipeline support, which was introduced in MongoDB version 4.2, helps us to perform some complex updates. Using pipeline support, we can write multi-stage update expressions, where the output of a stage is provided as input to the next stage. It also allows us to use field references and aggregation operators. We also learned how to manipulate elements in array fields, how to add, remove, and update elements in an array, how to sort an array, and how to add only unique elements to an array.

In the next chapter, we will learn about MongoDB aggregation framework and pipeline in detail.

lock icon The rest of the chapter is locked
You have been reading a chapter from
MongoDB Fundamentals
Published in: Dec 2020 Publisher: Packt ISBN-13: 9781839210648
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}