mongoose中的嵌套对象模式

4

假设我有这样一个架构:

var Language = new Schema({
    name: { type: String, unique: true, required: true },
    pos: [{
        name: String,
        attributes: [{
            name: String
        }]
    }]
});

posattributes中的每个项目都会有一个_id吗?如果我给pos数组中的name字段添加唯一索引,那么唯一性将仅针对该数组强制执行,还是所有条目都是唯一的?

2个回答

3

嵌入式文档(如posattributes)没有自己的模式,因此没有_id属性。

如果在pos数组中的name字段上添加唯一索引,则会在整个集合中强制实施唯一性,但不会在单个文档的数组内实现唯一性。请参见此帖子


这个在Mongoose 4中是不正确的,因为posattributes将会有它们自己的模式。 - Adam Lockhart

1
在Mongoose 4中,posattributes将有自己的模式。您可以通过以下方式防止它们获得_id属性:
// ...
    attributes: [{
        name: String,
        _id: false
    }]
// ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接