Mongoose保存子文档数组
Mongoose save subdocument array
我正在努力保存子文档数组。只是不会保存:
前端发送一个Day对象,该对象有一个标题数组,每个标题都可以有一个内容数组(这是一个mongoose模式(。
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一天的代码,以及相关的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.
现在有两种方法可以解决你的问题。
- 在修改数组索引时显式使用.set((
例如:
doc.someArray.set('3', 'changed');
doc.save();
这里名为someArray
的数组正在更改,因此我们需要使用array.set方法对其进行更改。
缺点:数组中的每个元素都需要使用array.set()
进行设置,因此,例如,数组中的5个元素发生更改时,您需要使用array.set
来设置所有元素。
2.将数组标记为已修改,然后保存。
例如:
doc.array[3] = 'changed';
doc.markModified('array');
这是另一种方法,当您不能显式调用array.set
方法时。例如:在我的案例中,我使用loadash合并文档并保存它,但我无法使用array.set
,因此我使用了array.markModified
。
Benifit:您不需要将数组中的每个元素设置为上述array.set
方法。
缺点:
如果是每个数组,则在保存之前必须将其标记为已修改。例如:如果您的文档中有10个数组,则在保存之前需要将它们全部标记为已修改:例如,
doc.markModified('array1');
doc.markModified('array2');
doc.markModified('array3');
在mongoose中仍然没有将多个数组标记为已修改的选项。我在上面分享的链接中请求了这个功能。
因此,这将类似于:
profile.markAllModified(['arrayFirst','arraySecond','arrayThird','arrayFourth']);
它已经被添加到猫鼬的里程碑4.0.7中,希望我们很快看到这个功能。
所以我假设Mongoose会保存子文档,只要它们有_id字段,但显然不是。保存子文档数组的解决方案是
- 从json为数组中的每个子文档创建一个新的Model对象
- 对每个对象调用save((
- 父对象并不关心子文档的_id是否存在——它可以在我将子文档本身保存到数据库之前存储它
同样,修复只是在子文档上调用.save((。
var content = new Content(c);
content.save();
h.content[y] = content;
exports.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();
}
};
- MongoDB (php) - 以数组而不是多个属性的形式返回文档属性
- 将当前用户的 ID 推送到 meteorjs 中集合/文档的内部数组
- 如何从 URL 数组向文档添加图像列表
- 将子文档推送到父数组时遇到问题
- 如何在mongodb(mongose)中覆盖子文档的数组属性
- 返回一个关联数组,而不是mongoose中的文档数组
- 如何获取文档数组中元素的计数-MongoDB
- node.js使用monk从数组中按id获取文档
- MongoDB findAndModify:>>>查找和更新文档数组中的对象
- 获取由 javascript 读取的数组文本字段值
- 更新数组中的嵌入文档 - Mongodb + Node Driver
- 从 JSON 对象数组中获取与条件匹配的文档
- 查找文档,然后在数组中查找条目并将其从数组中删除
- findAndModify,如何对文档进行操作's数组搜索对象并更改字段值
- 查询以匹配数组的第n个文档
- 使用 shift 在具有字符串/数组的文档中查找元素
- 返回一组文档,其中所有子数组都包含环回或nodejs中的给定值
- php数组转换为javascript数组文档.write javascript_array
- 在模型中插入一组文档
- 单击显示数组文本字段值