1

I have a collection. Each has a field studentUnitList which is Array of object. Inside this we have studentTopicList which is again Array of object. Structure of the document is:

{ 
    "_id" : "", 
    "studentId" : "", 
    "subjectId" : "", 
    "completionPercentage" : 0.0, 
    "subjectName" : "", 
    "numberOfVideos" : NumberInt(312), 
    "catalogueId" : "",
    "totalVideoLengthInSec" : NumberLong(209301), 
    "lastVisitedDate" : ISODate("2019-09-07T16:55:35.862+0000"), 
    "studentCatalogueId" : "", 
    "lastWatchedSubTopic" : {
        "studentUnitId" : "", 
        "studentTopicId" : "", 
        "studentSubTopicId" : "", 
        "videoCompletionInSeconds" : NumberLong(12)
    }, 
    "studentUnitList" : [
        {
            "_id" : "", 
            "unitId" : "", 
            "unitName" : "", 
            "completionPercentage" : 0.0, 
            "totalVideoLengthInSec" : NumberLong(38024), 
            "studentTopicList" : [
                {
                    "_id" : "", 
                    "topicId" : "", 
                    "topicName" : "", 
                    "completionPercentage" : 0.0, 
                    "totalVideoLengthInSec" : NumberLong(2151), 
                    "studentContents" : [
                        {
                            "contentType" : "SUBTOPIC", 
                            "subTopic" : {
                                "_id" : "", 
                                "subTopicId" : "", 
                                "subTopicName" : "", 
                                "pageContentId" : "", 
                                "videoLengthInSeconds" : NumberLong(2151), 
                                "videoCompletionInSeconds" : NumberLong(0), 
                                "videoCompletionPercentage" : 0.0
                            }
                        }
                    ]
                }, 
                {
                    "_id" : "", 
                    "topicId" : "", 
                    "topicName" : "", 
                    "completionPercentage" : 0.0, 
                    "totalVideoLengthInSec" : NumberLong(1903), 
                    "studentContents" : [
                        {
                            "contentType" : "SUBTOPIC", 
                            "subTopic" : {
                                "_id" : "", 
                                "subTopicId" : "", 
                                "subTopicName" : "", 
                                "pageContentId" : "", 
                                "videoLengthInSeconds" : NumberLong(491), 
                                "videoCompletionInSeconds" : NumberLong(0), 
                                "videoCompletionPercentage" : 0.0
                            }
                        }
                    ]
                }
            ]
        }
    ]
}

I want to add a boolean field "active" to each and every object of studentUnitList, studentTopicList, subTopic. After adding field document will look like:

{ 
    "_id" : "", 
    "studentId" : "", 
    "subjectId" : "", 
    "completionPercentage" : 0.0, 
    "subjectName" : "", 
    "numberOfVideos" : NumberInt(312), 
    "catalogueId" : "",
    "totalVideoLengthInSec" : NumberLong(209301), 
    "lastVisitedDate" : ISODate("2019-09-07T16:55:35.862+0000"), 
    "studentCatalogueId" : "", 
    "lastWatchedSubTopic" : {
        "studentUnitId" : "", 
        "studentTopicId" : "", 
        "studentSubTopicId" : "", 
        "videoCompletionInSeconds" : NumberLong(12)
    }, 
    "studentUnitList" : [
        {
            "_id" : "", 
            "unitId" : "", 
            "unitName" : "", 
            "completionPercentage" : 0.0, 
            "totalVideoLengthInSec" : NumberLong(38024), 
            "active": true,
            "studentTopicList" : [
                {
                    "_id" : "", 
                    "topicId" : "", 
                    "topicName" : "", 
                    "completionPercentage" : 0.0, 
                    "totalVideoLengthInSec" : NumberLong(2151), 
                    "active": true,
                    "studentContents" : [
                        {
                            "contentType" : "SUBTOPIC", 
                            "subTopic" : {
                                "_id" : "", 
                                "subTopicId" : "", 
                                "subTopicName" : "", 
                                "pageContentId" : "", 
                                "active": true,
                                "videoLengthInSeconds" : NumberLong(2151), 
                                "videoCompletionInSeconds" : NumberLong(0), 
                                "videoCompletionPercentage" : 0.0
                            }
                        }
                    ]
                }, 
                {
                    "_id" : "", 
                    "topicId" : "", 
                    "topicName" : "", 
                    "completionPercentage" : 0.0, 
                    "totalVideoLengthInSec" : NumberLong(1903), 
                    "active": true,
                    "studentContents" : [
                        {
                            "contentType" : "SUBTOPIC", 
                            "subTopic" : {
                                "_id" : "", 
                                "subTopicId" : "", 
                                "subTopicName" : "", 
                                "pageContentId" : "", 
                                "active": true,
                                "videoLengthInSeconds" : NumberLong(491), 
                                "videoCompletionInSeconds" : NumberLong(0), 
                                "videoCompletionPercentage" : 0.0
                            }
                        }
                    ]
                }
            ]
        }
    ]
}

I need a query or aggregation script to do the job.

1 Answer 1

2

Try $[] positional operator, The all positional operator $[] indicates that the update operator should modify all elements in the specified array field.

db.collection.updateMany({},
{
  $set: {
    "studentUnitList.$[].active": true,
    "studentUnitList.$[].studentTopicList.$[].active": true,
    "studentUnitList.$[].studentTopicList.$[].studentContents.$[].subTopic.active": true
  }
})

Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.