【发布时间】:2017-10-06 12:43:54
【问题描述】:
我有一个类似如下的架构:
const userSchema = mongoose.Schema({
// Other properties here
userMaterials: [{ title: String, category: String, id: String }]
});
用户应该发布一个材料,每个材料都添加到 userMaterial 数组中。
材质是一个物体,看起来像这样:
let material = {
id: dbMaterial.id.toString(),
title: dbMaterial.title,
category: req.body.category
};
保存两次(只是为了测试会发生什么)相同的材质对象后,我在数据库中发现的是一个额外的_id。
userMaterials:
[ { _id: 59105a5810400d7cde1d8e50,
category: 'want-to-watch',
title: 'jQuery Fundamentals',
id: 590f7826f55f2e2814d113f8 },
{ _id: 59105df83369cd7fa49c9ac1,
category: 'want-to-watch',
title: 'jQuery Fundamentals',
id: 590f7826f55f2e2814d113f8 } ]
我没想到额外的 _id 字段,数组内的对象不是子架构或其他东西。顺便说一句,当架构更改为 userMaterials: [] 一切都很好我得到了我实际期望的结果 - 未修改的材质对象被推入数组。
那么你能解释一下是什么让 mongoose 创建了 _id 字段吗??
【问题讨论】: