3.13 Mongoose的复杂操作
Mongoose 内部设置表字段的嵌套
互相嵌套
// 对象嵌套
const postSchema = new Schema({
name: String,
// 在这里引用的User 定义type 为ObjectId ref 为User
postedBy: {type: mongoose.Schema.Types.ObjectId, ref: 'User'}, //这里和Model同名
dateCreated: Date,
comments: [{body:"string", by: mongoose.Schema.Types.ObjectId}],
});
mongoose.model("Post",postSchema)
// 在User 中
// 数组嵌套
const UserSchema =new Schema({
name:String,
userID:String,
posts:[{ // 在USer 中这样定义Post
type: Schema.Types.ObjectId, ref: 'Post' //这里和Model同名
}]
})互相关联对应
Mongoose内的管道查询 aggregate
联表操作population
数组内部嵌套查询
数组内部的更新
数组内部的添加和删除
添加
删除
事物的支持
对象内部的CRUD
Last updated