Reader small image

You're reading from  MongoDB Fundamentals

Product typeBook
Published inDec 2020
PublisherPackt
ISBN-139781839210648
Edition1st Edition
Tools
Concepts
Right arrow
Authors (4):
Amit Phaltankar
Amit Phaltankar
author image
Amit Phaltankar

Amit Phaltankar is a software developer and a blogger experienced in building lightweight and efficient software components. He specializes in wiring web-based applications and handling large-scale data sets using traditional SQL, NoSQL, and big data technologies. He is experienced in many technology stacks and loves learning and adapting to newer technology trends. Amit is passionate about improving his skill set and loves guiding and grooming his peers and contributing to blogs. He is also an author of MongoDB Fundamentals.
Read more about Amit Phaltankar

Juned Ahsan
Juned Ahsan
author image
Juned Ahsan

Juned Ahsan is a software professional with more than 14 years of experience. He has built software products and services for companies and clients such as Cisco, Nuamedia, IBM, Nokia, Telstra, Optus, Pizzahut, AT&T, Hughes, Altran, and others. Juned has a vast experience in building software products and architecting platforms of different sizes from scratch. He loves to help and mentor others and is a top 1% contributor on StackOverflow. He is passionate about cognitive CX, cloud computing, artificial intelligence, and NoSQL databases.
Read more about Juned Ahsan

Michael Harrison
Michael Harrison
author image
Michael Harrison

Michael Harrison started his career at the Australian telecommunications leader Telstra. He worked across their networks, big data, and automation teams. He is now a lead software developer and the founding member of Southbank Software, a Melbourne based startup that builds tools for the next generation of database technologies.
Read more about Michael Harrison

Liviu Nedov
Liviu Nedov
author image
Liviu Nedov

Liviu Nedov is a senior consultant with more than 20 years of experience in database technologies. He has provided professional and consulting services to customers in Australia and Europe. Throughout his career, he has designed and implemented large enterprise projects for customers like Wotif Group, Xstrata Copper/Glencore, and the University of Newcastle and Energy, Queensland. He is currently working at Data Intensity, which is the largest multi-cloud service provider for applications, databases, and business intelligence. In recent years, he is actively involved in MongoDB NoSQL database projects, database migrations, and cloud DBaaS (Database as a Service) projects.
Read more about Liviu Nedov

View More author details
Right arrow

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 2020Publisher: PacktISBN-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.
undefined
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 €14.99/month. Cancel anytime

Authors (4)

author image
Amit Phaltankar

Amit Phaltankar is a software developer and a blogger experienced in building lightweight and efficient software components. He specializes in wiring web-based applications and handling large-scale data sets using traditional SQL, NoSQL, and big data technologies. He is experienced in many technology stacks and loves learning and adapting to newer technology trends. Amit is passionate about improving his skill set and loves guiding and grooming his peers and contributing to blogs. He is also an author of MongoDB Fundamentals.
Read more about Amit Phaltankar

author image
Juned Ahsan

Juned Ahsan is a software professional with more than 14 years of experience. He has built software products and services for companies and clients such as Cisco, Nuamedia, IBM, Nokia, Telstra, Optus, Pizzahut, AT&T, Hughes, Altran, and others. Juned has a vast experience in building software products and architecting platforms of different sizes from scratch. He loves to help and mentor others and is a top 1% contributor on StackOverflow. He is passionate about cognitive CX, cloud computing, artificial intelligence, and NoSQL databases.
Read more about Juned Ahsan

author image
Michael Harrison

Michael Harrison started his career at the Australian telecommunications leader Telstra. He worked across their networks, big data, and automation teams. He is now a lead software developer and the founding member of Southbank Software, a Melbourne based startup that builds tools for the next generation of database technologies.
Read more about Michael Harrison

author image
Liviu Nedov

Liviu Nedov is a senior consultant with more than 20 years of experience in database technologies. He has provided professional and consulting services to customers in Australia and Europe. Throughout his career, he has designed and implemented large enterprise projects for customers like Wotif Group, Xstrata Copper/Glencore, and the University of Newcastle and Energy, Queensland. He is currently working at Data Intensity, which is the largest multi-cloud service provider for applications, databases, and business intelligence. In recent years, he is actively involved in MongoDB NoSQL database projects, database migrations, and cloud DBaaS (Database as a Service) projects.
Read more about Liviu Nedov