How can I insert a new entry inside Multilevel structure of mongoDB database?
Asked Answered
C

1

0

I am working on an app that contain details of an academic year in Multilevel structure of mongoDB database.

For this I've created structure like this:

[
{
        "_id" : ObjectId("5a1519a71fe8cc4df5888ff5"),
        "academicyear" : "2016-2017",
        "departments" : [
                {
                        "deptname" : "computer",
                        "semesters" : [
                                {
                                        "sem" : "2",
                                        "classes" : [
                                                {
                                                        "class" : "1",
                                                        "theory" : [
                                                                {
                                                                        "subname" : "DS",
                                                                        "faculty" : [
                                                                                {
                                                                                        "facultyname" : "KSS",
                                                                                        "facultydept" : "COMP"
                                                                                }
                                                                        ]
                                                                }
                                                        ],
                                                        "practical" : [
                                                                {
                                                                        "subname" : "DS",
                                                                        "batch" : [
                                                                                {
                                                                                        "bname" : "C",
                                                                                        "facultyname" : "KSS",
                                                                                        "facultydept" : "COMP"
                                                                                }
                                                                        ]
                                                                }
                                                        ]
                                                }
                                        ]
                                }
                        ]
                }
        ]
}


]

Now for the same academic year and for the same department I would like to create a new semester node inside the array of semesters. For that I've tried syntax of $ and use of . operator but it did not worked well. How can I add a new semester node inside that semesters array? Also after creating a new semester node, how can I add a new class in an classes array?

Corvine answered 22/11, 2017 at 8:9 Comment(1)
Please read Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions.Kabob
T
0

it could be done by means of the positional operator $[],

db.collection.update(
    { "academicyear": "2016-2017", "departments.deptname": "computer"},
    { "$push": 
        {"departments.$[].semesters": 
            {
                "sem" : "2",
                "classes" : [ ...
            }
        }
    }
)

It could also be done by indicating the position in the array if known. "departments.1.semesters", where 1 is the position of 'departments' in the array.

db.collection.update(
    { "academicyear": "2016-2017"},
    { "$push": 
        {"departments.1.semesters": 
            {
                "sem" : "2",
                "classes" : [ ...
            }
        }
    }
)

One way of doing it would be:

var toInsert = {"sem" : "2", "classes" : [ ... }
db.collection.update(
            { "academicyear": "2016-2017", departments: { $elemMatch: { deptname: "computer" } } },
            { $addToSet: { 'departments.$.semesters': toInsert } }
  )

The query part will find the document from the list array with id = 2. Then we use $ positional element to add a new element at that array index.

and the same would apply for classes.

db.collection.update(
    { "academicyear": "2016-2017"},
    { "$push": 
        {"departments.1.semesters.0.classes": 
            {
                "class" : "1",
                "theory" : [{ ...
            }
        }
    }
)

Another alternative would be to extract the document, update it on the returned object and save it again.

db.collection.findOne({"academicyear": "2016-2017", "departments.deptname": "computer"}, {'departments.semesters': 1}, (err, doc) => {
        if(err) return handleError(res)(err); 
        if(!doc) return res.send(404).end();

        //I guess semester is 0, but you can find the one that you are going to update by some filter
        doc.departments[0].semesters[0].classes.push({...});

          doc.save( (err) => {
            if(err) return handleError(res)(err)

            return res.status(200).end();
          });  

      });
Tridactyl answered 1/11, 2018 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.