繁体   English   中英

Mongoose 保存子文档数组

[英]Mongoose save subdocument array

我正在努力保存子文档数组。 只是不会保存:

前端发送一个 Day 对象,它有一个标题数组,每个标题可以有一个内容数组(这是一个猫鼬模式)。

var DaySchema = mongoose.Schema({
date: Date,
startTime: Date,
endTime: Date,
title: String,
order: Number,
description: String,
cohort: {
    type: objId,
    ref: 'cohort'
},
instructors: [{
    type: objId,
    ref: 'instructor'
}],
headings: [{
    title: String,
    subTitle: String,
    content: [{
        type:objId,
        ref: 'content'
    }]
}]
});


var ContentSchema = mongoose.Schema({
title: String,
description: String,
contentUrl: String,
dueDate: Date,
dateAssigned: Date,
downloadUrl: String
});

这是我用来 PUT 和 POST a Day 的代码及其关联的 Content 对象。 除了保存内容外,一切正常。 当我查看 mongo shell 时,标题下的每个内容字段如下所示:

content: []

有什么想法吗?

exports.putdaycontent = function (req, res) {

var dayId = req.params.dayId;
var headings = req.body.headings;
var day = req.body;

var updateDay = function () {
    Day.update({_id: dayId}, day, {new: true, upsert: true}, function (err, day) {
        if (err)
            error.databaseError(req, res, err);

        res.send(day);
    });
};

if (headings) {
    for (var x = 0; x < headings.length; x++) {
        var h = headings[x];

        if (h.content) {
            for (var y = 0; y < h.content.length; y++) {
                var c = h.content[y];

                //If existing content update it
                if (c._id && c._id.length > 0) {
                    Content.update({_id: c._id}, c, {new: true,upsert: true}, function (err, content) {

                    });
                } else {
                    var content = new Content(c);
                    h.content[y] = content;
                }

            }
        }
    }
    day.headings = headings;
    updateDay();

} else {
    updateDay();
}


};

exports.postdaycontent = function (req, res) {

var headings = req.body.headings;
var day = req.body;
var cohortId = day.cohort._id;

var postDay = function () {
    var d = new Day(day);
    console.log("CONTENT DAYS:",d);
    d.save(function(err, newDay) {
       if (err)
           error.databaseError(req, res, err);

        Cohort.findOneAndUpdate({_id: cohortId}, {$push: {days: newDay}}, {upsert: true}, function(err, newCohort) {
            res.send({msg:"Successfully saved day to cohort"});
        });
    });
};

if (headings) {
    for (var x = 0; x < headings.length; x++) {
        var h = headings[x];

        if (h.content) {
            for (var y = 0; y < h.content.length; y++) {
                var c = h.content[y];

                var content = new Content(c);
                h.content[y] = content;
                console.log("CONTENT:",content);

            }
        }
    }
    day.headings = headings;
} 

postDay();
};

这是我几天前在 2015 年 6 月 22 日遇到的问题。这是我在 github https://github.com/Automattic/mongoose/issues/3093上提出的问题的链接。


现在有两种解决方案可以解决您的问题。

  1. 每当您修改数组索引时,都明确使用 .set()

例如:

 doc.someArray.set('3', 'changed'); doc.save();

这里名为someArray的数组正在更改,因此我们需要使用 array.set 方法更改它。

缺点:数组的每个元素都需要使用array.set()设置,因此例如,数组中的 5 个元素发生变化,您需要使用array.set设置所有array.set


2. 将数组标记为已修改,然后保存。

例如:

 doc.array[3] = 'changed'; doc.markModified('array');

这是另一种方式,当您不能显式调用array.set方法时。 例如:在我的情况下,我使用 loadash 合并文档并保存它,我无法使用array.set ,因此我使用了array.markModified

好处:您不需要像上面的array.set方法一样将数组中的每个元素设置为。

退税:

对于每个数组,您需要在保存之前将其标记为已修改。 例如:如果您的文档中有 10 个数组,则需要在保存之前将它们全部标记为已修改:例如,

 doc.markModified('array1'); doc.markModified('array2'); doc.markModified('array3');


在 mongoose 中仍然没有选项可以将多个数组标记为已修改。 我已经在上面分享的链接中请求了此功能。

所以这将是这样的:

 profile.markAllModified(['arrayFirst','arraySecond','arrayThird','arrayFourth']);

它已添加到 mongoose 的里程碑 4.0.7,希望我们能尽快看到此功能。

所以我假设 Mongoose 会保存子文档,只要它们有 _id 字段 - 但显然不是。 保存子文档数组的解决方案是

  1. 从 json 为数组中的每个子文档创建一个新的 Model 对象
  2. 对每个对象调用 save()
  3. 父对象不关心子文档的 _id 是否存在 - 它可以在我什至将子文档本身保存在数据库中之前存储它。

再次修复只是在子文档上调用 .save() 。

var content = new Content(c); content.save(); h.content[y] = content;

export.putdaycontent = 函数 (req, res) {

var dayId = req.params.dayId;
var headings = req.body.headings;
var day = req.body;

var updateDay = function () {
    Day.update({
        _id: dayId
    }, day, {
        new: true,
        upsert: true
    }, function (err, day) {
        if (err)
            error.databaseError(req, res, err);

        res.send(day);
    });
};

if (headings) {
    for (var x = 0; x < headings.length; x++) {
        var h = headings[x];

        if (h.content) {
            for (var y = 0; y < h.content.length; y++) {
                var c = h.content[y];

                //If existing content update it
                if (c._id && c._id.length > 0) {
                    Content.update({
                        _id: c._id
                    }, c, {
                        new: true,
                        upsert: true
                    }, function (err, content) {

                    });
                } else {
                    var content = new Content(c);
                    content.save();
                    h.content[y] = content;
                }

            }
        }
    }
    day.headings = headings;
    updateDay();

} else {
    updateDay();
}


 };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM