nodejs 进行 mongoDB 两数据表表关联查询——联表查询

zhuanbike 2021-12-27 847

1.创建一个 user(被关联的表)

const UserSchema = new mongoose.Schema({undefined
name: { // 用户姓名
type: String,
},
loginname: { // 用户姓名
type: String,
},
});
const TABLE_NAME=“users”;
const DATABASE_NAME=“node_dev”;
mongoose.model(‘users’, UserSchema );

2.创建一个 article

var Schema = mongoose.Schema;
var articleSchema = new Schema( {undefined
content: {type: String},
author_id: {undefined
type: Schema.ObjectId,
ref:‘users’ //关联用户表
},
}
);
find(obj).sort(sort).skip(skip).limit(pageSize).populate(“author_id”);

注:author_id则变成一个对象直接取对象属性即可


最新回复 (0)
发新帖