Database Reference
In-Depth Information
Figure 5-4. Rename “Bop” to “BeBop”
First, we'll update the category name with the following operation:
db
.
categories
.
update
(
{
'_id'
:
bop_id
}, {
'$set'
: {
'name'
:
'BeBop'
} } )
Next, we'll update each descendant's ancestors list:
db
.
categories
.
update
(
{
'ancestors._id'
:
bop_id
},
{
'$set'
: {
'ancestors.$.name'
:
'BeBop'
} },
multi
=
True
)
There are a couple of things to know about this update:
▪ We've used the positional operation
$
to match the exact “ancestor” entry that matches the
query.
▪ The
multi
option allows us to update all documents that match this query. By default,
MongoDB will only update the first document that matches.