如何在mongoDB数据库的多级结构内插入新条目? [英] How can I insert a new entry inside Multilevel structure of mongoDB database?

查看:434
本文介绍了如何在mongoDB数据库的多级结构内插入新条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,该应用程序包含mongoDB数据库的多级结构中一学年的详细信息.

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"
                                                                                }
                                                                        ]
                                                                }
                                                        ]
                                                }
                                        ]
                                }
                        ]
                }
        ]
}


]

现在,对于同一学年和同一部门,我想在一系列学期内创建一个新的学期节点.为此,我尝试使用$的语法和使用.运算符,但效果不佳.如何在该学期数组中添加一个新的学期节点?另外,在创建新的学期节点之后,如何在classes数组中添加新的课程?

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?

推荐答案

可以通过位置运算符$ [],

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" : [ ...
            }
        }
    }
)

也可以通过指示数组中的位置(如果已知)来完成. "departments.1.semesters",其中1是部门"在数组中的位置.

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" : [ ...
            }
        }
    }
)

一种实现方法是:

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

查询部分将从id = 2的列表数组中找到文档.然后,我们使用$ positional元素在该数组索引处添加一个新元素.

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.

,同样适用于课程.

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();
          });  

      });

这篇关于如何在mongoDB数据库的多级结构内插入新条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆