2012-08-11 66 views
4

让我们假设我有这样嵌套对象模式

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

的模式将在pos每个项目,并在attributes,有_id?如果我向pos数组中的name字段添加唯一索引,那么将对该数组执行唯一性还是对所有条目都是唯一的?

回答

3

没有,像posattributes嵌入文档不拥有自己的模式不具有_id财产。

如果您在pos阵列中添加唯一索引到name场,唯一性会在整个收集被强制执行,但不一个单个文档的数组中。见this post

+0

这是不正确的作为Mongoose 4,其中'pos'和'属性'将得到自己的模式。 – 2016-04-22 13:25:10

1

在Mongoose 4中,posattributes将获得他们自己的模式。你可以防止他们得到_id这样的属性:

// ... 
    attributes: [{ 
     name: String, 
     _id: false 
    }] 
// ...