Re: updating nested array elements

From: Lavi Bharath <lavibharath@xxxxxxxxx>
To: mongodb-user <mongodb-user@xxxxxxxxxxxxxxxx>
Date: Thu, 12 May 2016 23:52:12 -0700 (PDT)
Why ads?
Thanks Wan.

On Friday, May 13, 2016 at 2:15:27 PM UTC+8, Wan Bachtiar wrote:

Hi Lavi, 

Currently, as of version v3.2 you won’t able to update nested arrays using 
$set <https://docs.mongodb.org/manual/reference/operator/update/set/>. 
There is an open ticket in MongoDB jira tracker SERVER-831 
<https://jira.mongodb.org/browse/SERVER-831> to support $set for nested 
arrays, please feel free to watch and/or upvote 
<https://confluence.atlassian.com/jira/watching-and-voting-on-an-issue-185729526.html
for updates.

One work-around is to use .forEach 
<https://docs.mongodb.com/manual/reference/method/cursor.forEach/> to 
iterate through the arrays and update the document, example:

db.datacomplete.find({
    "test":"Milan", "analysis.analysis_id" : "2016-05-09T18-35-23.601813", "analysis.per_data_status.id" : "9009"})
    .forEach(
        function(doc){
            doc.analysis.forEach(function(a){ 
                if(a.analysis_id=="2016-05-09T18-35-23.601813"){
                    a.per_data_status.forEach(function(data){
                        if(data.id=="9009"){
                            data.StatsSubmission = "SUCCESS"; 
                            data.ArchiveSubmission = "DONE";
                        }
                    })
                }
            })
            db.datacomplete.save(doc) 
        })

Alternatively, you can modify the document schema to remove nested arrays. 
For example, depending on your application use case, you could store 
per_data_status into another collection. Another example to try is to use 
sub-document rather than array for analysis: 

{
  "_id": ObjectId("57304a006f42802df624aaa6"),
  "test": "Milan",
  "timestamp": NumberLong("1462781256018"),
  "analysis": {    
      /* Use username as the key per analysis */
      "xxx" : {
          "Status": "SUCCESS",
          "startTime": "2016-05-09T18-35-23.601813",
          "EndTimes": "2016-05-09T18-40-02.891451",
          "analysis_id": "2016-05-09T18-35-23.601813",
          "per_data_status": [
          {
            "id": "9009",
            "dir": "Project_9009",
            "Status": "SUCCESS",
            "StatsSubmission": "SUCCESS",
            "ArchiveSubmission": "TODO"
          }
        ]    
     }
   }
}

Different schemas have different benefits depending on your application 
use case. You may find the following useful:

   - Data Model Examples and Patterns 
   <https://docs.mongodb.com/manual/applications/data-models/
   - Operational Factors and Data Models 
   <https://docs.mongodb.com/manual/core/data-model-operations/

Also to point out you may want to store the date values as BSON Date 
<https://docs.mongodb.com/manual/reference/bson-types/#document-bson-type-date
via Date() <https://docs.mongodb.com/manual/reference/method/Date/>. 
Especially if you would like to query using date values later on.

Regards, 

Wan.



-- 
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
--- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@xxxxxxxxxxxxxxxx.
To post to this group, send email to mongodb-user@xxxxxxxxxxxxxxxx.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/5d40dc92-9b02-426b-a752-cda914d050fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Why ads?