How to update item on multi-level array in a document?

Since I am from SQL background I have designed an document hierarchy as below with nested arrays.

How can I simplify this model for different use cases & how to update item in 2nd level array?

{
           _id: 1,
           Name: "My Job 1",
           ExecutionTriggers: [
                      {
                           IsActive: true,
                           startOn: "12:00 AM",
                           ExecutionResults: [
                                      { 
                                               DateExecuted: "09/11/2021", 
                                               IsSuccess: false, 
                                               Message: 'API failed to respond.' 
                                      },
                                      { 
                                               DateExecuted: "10/11/2021", 
                                               IsSuccess: true, 
                                               Message: '' 
                                      }
                            ],
                          APIVersion: 2.1
                      },
                      {
                           IsActive: false,
                           startOn: "03:00 AM",
                           ExecutionResults: [
                                      { 
                                               DateExecuted: "08/11/2021", 
                                               IsSuccess: false, 
                                               Message: 'No data to process.' 
                                      },
                                      { 
                                               DateExecuted: "09/11/2021", 
                                               IsSuccess: false, 
                                               Message: 'No data to process.' 
                                      }
                            ],
                          APIVersion: 1.8
                      }
           ]
}

As we see above,

  • document root has “ExecutionTriggers” array
  • “ExecutionTriggers” array has object which contains another array “ExecutionResults”

Use cases:

  1. I have run report frequently to get job execution status i.e. Execution Results - how to get this along with parent fields (job id, name, is active, start on & execution results)

  2. How I can push the object into this second level array?
    Now, I want to push new entry to the “ExecutionResults” array.

Since document has unique “_id” I can find the document, but the objects in “ExecutionTriggers” array has no unique identifiers.

So how to push item into doc_root.ExecutionTriggers[1].ExecutionResults?

Is it really good practice to have nested multi-level objects in MongoDB?

Hi @Gopinath_Ashokan ,

You need to use arrays filter clause to identify the member you want to manipulate and pust to it.

The following docs will help:

Specifically nested array section:

Let me know if you still confused.

Thanks
Pavel

1 Like

Hi @Pavel_Duchovny

Thanks for the reference, let me try this and let you know.

Regards
Gopinath