我有一个schema:
这个schema有一个sub doc 叫address,插入address数据之后,address会自己有一个对应的_id.
我的问题是,每次查询这个address的时候,是否需要首先找到这个account,然后遍历account的address,或者可以直接通过address的_id来找到对应的address?
var AccountSchema = new mongoose.Schema({
email: { type: String, unique: true },
password: { type: String},
phone: { type: String},
name: {type: String},
address: {type: [{
name: { type: String},
phone: { type: String},
type: { type: String},
addr: { type: String}
}]},
});
First you understand
subDoc
的定义就错了,subDoc
应该也是一个由单独的Schema
->Model
生成的实例, 简单来说, 就是得有一个子文档的Schema
One more thing, if you don’t have
自定义的 SchemaTypes
的话, 原来的写法就是错的. 而且就算定义了, 属性type
you can’t point to an objectBecause mongoose is legal by default
There must be a few in the document. Except In addition, if you do not define any custom Type, any other value after the type attribute will result in an error.SchemaTypes
SchemaTypes
就String, Number, Array, ObjectId, Mixed...
justString, Number, Array, ObjectId, Mixed...