Insert data in embeded document MongoDB
Posted By : Udit Kaul | 24-May-2016
There are situations where you need to enter data in the embeded document in MongoDB in that case the following information might come in handy.
$push
The $push adds specified value to an array.
$push operator can be used as follows:
{$push:{<field1>:<value1>,<value2>,..}}
When specifiying <field> attribute in an embeded document or in an array use dot notation
Properties
If the value is an array,$push operator appends the whole array as a single element.To add each element of value seperately use $each
Modifiers
You can use the $push operator with the following modifiers:
Modifiers Description
$each Adds multiple values to the array fields.
$slice Restricts the number of array elements,requires the use of $each modifier.
$sort Orders the elements of the array,requires the use of $each modifier.
$position Specifies the location in the array where the new element are to be inserted,requires the use of $each modifier
When using the above modifiers as well the $push is wriiten as follows:
{$push:{<field1>:{<modifier1>:<value1>,<value2>,..},}}
The processing of $push operation happens in the following order when used with modifiers:
1)Update array to add elements in the correct position
2)Apply sort ,if specified
3)Sice the array,if specified
4)Store the array
Examples for various situations:
1)Append values to an array:
The following array appends 90 to the scores array
db.students.update({_id:1},{$push:{scores:90}})
2)Append multiple values to an array
Use $push with the $each modifier to append multiple values to the array field
The following example appends [90,92,85] to the scores array for the document where the name field equals rahul
db.students.update({name:"rahul"},{$push:{scores{$each:[90,92,85]}}})
3)$push operation with multiple modifiers
A collection students has the following document:
{
"_id" : 5,
"quiz" : [
{ "wk": 1, "score" : 10 },
{ "wk": 2, "score" : 8 },
{ "wk": 3, "score" : 5 },
{ "wk": 4, "score" : 6 }
]
}
The following $push operation uses:
the $each modifier to add multiple documents to the quiz array,
the $sort modifier to sort all the elements of the modified quiz array by the score field in descending order, and
the $slice modifier to keep only the first two sorted elements of the quizzes array.
db.students.update(
{ _id: 5 },
{
$push: {
quiz: {
$each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ],
$sort: { score: -1 },
$slice: 2
}
}
}
)
The result of the operation is keep only the two highest scoring quizzes:
{
"_id" : 5,
"quiz" : [
{ "wk" : 1, "score" : 10 },
{ "wk" : 2, "score" : 8 },
{ "wk" : 5, "score" : 8 }
]
}
4)pushing data to a particular location in the array
Combination of $push ,$each and $postion operator can be used to insert data at a particular location in the array
For example:
db.students.update({name:rahul},{$push:{scores:{$each:[90,92,85],$postion:1}}})
This command will insert data [90,92,85] at the 1st postion in the scores array
Hope that the information was helpful,
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Udit Kaul
Udit is a Bright Web App developer, he is OCJP ,OCWCD and OCPLSQL certificated. He has good knowledge of Java and SQL. He likes playing football and very much interested in other sports too.