对于我正在处理的节点js app,我有以下Mongoose模式:
var memory = new schema({
date_added: Date,
name: String,
description: String,
personsInvolved: [{person: String}],
location: String,
uniqueness: Number,
category: String
});
它有一个名为personsInvolved的数组字段,其中只有一个字段,person。
在我的应用程序中,有一个表单,其中包含一个以逗号分隔的人员列表,然后转到创建函数,该函数获取该值并将其在逗号上拆分为数组。
该部分有效,但是当我将文档保存到mongo时,它会为数组中的每个人添加一个_id字段。它看起来像这样:
personsInvolved:
[ { person: 'Test', _id: 52c6d6c2457e5ce02b00000b },
{ person: ' test2', _id: 52c6d6c2457e5ce02b00000c },
{ person: ' test3', _id: 52c6d6c2457e5ce02b00000d } ] }
有没有办法让_id字段没有添加到每个人?这是我用来将记录保存到mongo的代码:
exports.create = function(req, res) {
// people are separated by a comma
var people = req.body.people;
var peopleArr = req.body.people.split(",");
var newMem = new memory();
newMem.date_added = Date.now();
newMem.name = req.body.name;
newMem.description = req.body.description;
for(var i in peopleArr) {
var name = {person: peopleArr[i]};
newMem.personsInvolved.push(name);
}
newMem.location = req.body.location;
newMem.uniqueness = req.body.uniqueness;
newMem.category = req.body.category;
console.log(newMem);
newMem.save(function(err, memory, count) {
res.redirect('/');
});
};
我只使用personInvolved as data,数组中的人员不会被用来识别任何其他文件。