Database Reference
In-Depth Information
At this point, our document, which once looked tidy and trustworthy, has been
transformed into something like this:
{
"Author" :
[
"Hows, David",
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin, Peter",
"Griffin, Brian",
"Griffin, Louis",
"Griffin, Meg"
],
"ISBN" : "1-4302-3051-7",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB, The",
"Type" : "Book",
"_id" : ObjectId("4c436231c603000000007ed0")
}
Removing Elements from an Array
MongoDB also includes several methods that let you remove elements from an array,
including $pop , $pull , $pullAll . In the sections that follow, you'll learn how to use each
of these methods for removing elements from an array.
The $pop operator lets you remove a single element from an array. This operator lets
you remove the first or last value in the array, depending on the parameter you pass down
with it. For example, the following snippet removes the last element from the array:
> db.media.update( { "ISBN" : "1-4302-3051-7" }, {$pop : {Author : 1 } } )
In this case, the $pop operator will pop Meg's name off the list of authors. Passing
down a negative number would remove the first element from the array. The following
example removes Peter Membrey's name from the list of authors:
> db.media.update( { "ISBN" : "1-4302-3051-7" }, {$pop : {Author : -1 } } )
Specifying a value of -2 or 1000 wouldn't change which element gets removed.
any negative number would remove the first element, while any positive number would
remove the last element. Using the number 0 removes the last element from the array.
Note
 
 
Search WWH ::




Custom Search