Database Reference
In-Depth Information
"ISBN" : "978-1-4302-5821-6",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB 2nd ed., The",
"Type" : "Book",
"_id" : ObjectId("4c436231c603000000007ed0")
}
Specifying Multiple Values in an Array
When working with arrays, the $push operator will append the value specified to the
given array, expanding the data stored within the given element. If you wish to add
several separate values to the given array, you can use the optional
$each
modifier as in
this example:
> db.media.update( { "ISBN" : "978-1-4302-5821-6" }, { $push: { Author : {
$each: ["Griffin, Peter", "Griffin, Brian"] } } } )
{
"Author" :
[
"Hows, David",
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim",
"Griffin, Stewie",
"Griffin, Peter",
"Griffin, Brian"
],
"ISBN" : "978-1-4302-5821-6",
"Publisher" : "Apress",
"Title" : "Definitive Guide to MongoDB 2nd ed., The",
"Type" : "Book",
"_id" : ObjectId("4c436231c603000000007ed0")
}
Optionally, you can use the
$slice
operator when using
$each
. This allows you to
limit the number of elements within an array during a
$push
operation.
$slice
takes
either a negative number or zero. Using a negative number ensures that only the