[英]Moogose append to the sub array document
I am having a problem with appending data into the sub document. 我在将数据附加到子文档中时遇到问题。
exports.appendfiles = function(req, res) {
project.findOneAndUpdate(
{
_id: req.body.id
},
{ $push:
{ 'files' : req.files }
},
{
safe: true,
upsert:true
},
function(err, data) {
console.log(err);
return res.send('ok');
}
);
};
This above code does append the data into the sub document, but however you can see the below output it appends a new item with a new index, which is not great. 上面的代码确实将数据附加到了子文档中,但是您可以看到下面的输出,它在新项目的后面附加了新索引,这并不好。 I kept my files model as files : Array any one can help? 我将文件模型保存为files:Array谁能帮上忙? Much appreciated. 非常感激。 the following json is what i have cut out from the document. 以下json是我从文档中删除的内容。
{
"buffer": null,
"truncated": false,
"size": 497328,
"extension": "csv",
"path": "uploads/dc45dfeb54c4be89968faa46aabeb114.csv",
"mimetype": "text/csv",
"encoding": "7bit",
"name": "dc45dfeb54c4be89968faa46aabeb114.csv",
"originalname": "obansocial2015-04-20 (7).csv",
"fieldname": "3"
},
{
"0": {
"buffer": null,
"truncated": false,
"size": 8855,
"extension": "html",
"path": "uploads/273bee3485fc564e80d80e92cef32215.html",
"mimetype": "text/html",
"encoding": "7bit",
"name": "273bee3485fc564e80d80e92cef32215.html",
"originalname": "Southern Stars Conference 2014.html",
"fieldname": "0"
},
"1": {
"buffer": null,
"truncated": false,
"size": 383631,
"extension": "mp4",
"path": "uploads/f32da61db7e8df6fccf97b65a788e39d.mp4",
"mimetype": "video/mp4",
"encoding": "7bit",
"name": "f32da61db7e8df6fccf97b65a788e39d.mp4",
"originalname": "video.mp4",
"fieldname": "1"
}
},
You are pushing an array into another, hence it is being nested. 您正在将一个数组推入另一个数组,因此它是嵌套的。
If you want to append each item, use the $each
operator: 如果要附加每个项目,请使用$each
运算符:
project.findOneAndUpdate(
{
_id: req.body.id
},
{ $push:
{ 'files' : {$each: req.files} }
},
{
safe: true,
upsert:true
},
function(err, data) {
console.log(err);
return res.send('ok');
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.