One of the biggest advantages of mongodb is the option of defining documents inside documents and by doing so creating powerful and at the same time flexible data structures.
In most cases, the deepest you will go is one level, even though mongo doesn’t set any limit on how many levels you can go inside a document, for example: A BlogPost can have an array of Comments, or an Image can have an array of Tags, and there is plenty of documentation online on how to manipulate single dimension arrays in documents. However, what if you have a multi dimensional array in a document?
Take for example the following model:

   
 /**  
 * Line Item schema  
 */  
 exports.EstimateLineItem = (function () {  
 schemas.lineItem = new Schema({  
 ‘name’ : String,  
 ‘description’ : String,  
 ‘quantity’ : Number,  
 ‘cost’ : Number  
 });

 return db.model(‘EstimateLineItem’, schemas.lineItem);  
 })();

/**  
 * Estimate schema  
 */  
 exports.Estimate = (function () {  
 schemas.estimate = new Schema({  
 ‘name’ : String,  
 ‘quoteID’ : Number,  
 ‘subTotal’ : Number,  
 ‘finalTotal’ : Number,  
 ‘creationDate’ : { type: Date, default: Date.now },  
 ‘status’ : { type: String, default: "Active" },  
 ‘lineItemSet’ : [schemas.lineItem]  
 });

 return db.model(‘Estimate’, schemas.estimate);  
 })();

/**  
 * Job schema  
 */  
 exports.Job = (function () {  
 schemas.job = new Schema({  
 ‘name’ : String,  
 ‘description’ : String,  
 ‘creationDate’ : { type: Date, default: Date.now },  
 ‘status’ : { type: String, default: "Active" },  
 ‘scheduledDates’ : [Date],  
 ‘customerID’ : ObjectId,  
 ‘estimateSet’ : [schemas.estimate]  
 });

 return db.model(‘Job’, schemas.job);  
 })();

So a Job has an array of Estimates and an Estimate has an array of LineItems.
The problem is, how to add a new LineItem to an Estimate?
One option would be to query for a job, then loop through its estimateSet and find the selected estimate, then push a new line item to the lineItemSet and finally save back the job. However, there is a simpler way:

   
 Job.update(  
 {estimateSet: {"$elemMatch": {_id: estimateID}}},  
 {$push:  
 {  
 "estimateSet.$.lineItemSet":  
 {  
 ‘name’ : lineItem.name,  
 ‘description’ : lineItem.description,  
 ‘quantity’ : parseInt(lineItem.quantity),  
 ‘cost’ : parseInt(lineItem.cost),  
 ‘_id': lineItem._id  
 }  
 }  
 },  
 {upsert:false,safe:true},  
 function (err) {  
 console.log("err: ", err);  
 if (err) {  
 res.send({  
 "err": true,  
 });  
 }  
 else {  
 res.send({  
 "err": false,  
 })  
 }  
 }  
 );

Breaking down the query:
It finds a job that has an Estimate with the specified ID:

{estimateSet: {"$elemMatch": {_id: estimateID}}}

Once the job is found, it pushes a new line item to the lineItemSet:

 
 {$push:  
 {  
 "estimateSet.$.lineItemSet":  
 {  
 ‘name’ : lineItem.name,  
 ‘description’ : lineItem.description,  
 ‘quantity’ : parseInt(lineItem.quantity),  
 ‘cost’ : parseInt(lineItem.cost),  
 ‘_id': lineItem._id  
 }  
 }  
 },  

For more info: