使用nodejs加入mongoose中的两个集合
Join two collection in mongoose using nodejs
我想加入 MongoDB
中的两个合集。我有两个合集 1. student 2. course.
学生合集:
课程合集
我尝试了一些代码,但没有用。
这是我的代码
student.js
router.get('/student/:id', function(req, res){
Student.find({_id:req.params.id}, function(err, user){
res.send(user);
})
})
这是架构:
student.js
const mongoose = require('mongoose');
let StudentSchema = mongoose.Schema({
name:{
type: String
},
email:{
type: String
},
phone:{
type: String
},
password:{
type: String
},
course :[{
type: mongoose.Schema.Types.ObjectId,
ref: 'Course'
}]
}, { collection: 'student' });
const Student = module.exports = mongoose.model('Student', StudentSchema);
course.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let CourseSchema = mongoose.Schema({
student_id:{
type: String
},
course_name:{
type: String
},
course_cost:{
type: String
}
}, { collection: 'course' });
const Course = module.exports = mongoose.model('Course', CourseSchema);
结果:
您需要这样查询:
findOne : to find single document (return object {} )
find : to find multiple documents (return array [])
Student.findOne({_id:req.params.id}, function(err, student){
if(!err && student){
Courses.find({student_id:req.params.id},function(error,courses){
if(!error && courses){
student.courses=courses;
}else{
student.courses=[];
}
res.send(student);
});
}
});
目前您正在获取 course :[]
,因为在学生集合中找不到字段,如您在照片 1 中所见
在学生集合中插入文档时需要设置 course :["coures_id1","coures_id2"]
。
然后您可以使用mongoose populate将课程填充到学生中
Student.findOne({_id:req.params.id}).populate('course').exec(function(err,student){
res.send(student);
});
因此,无需存储 student_id
课程集合中的字段,就像您从学生集合中获取的那样。
我想加入 MongoDB
中的两个合集。我有两个合集 1. student 2. course.
学生合集:
课程合集
我尝试了一些代码,但没有用。
这是我的代码
student.js
router.get('/student/:id', function(req, res){
Student.find({_id:req.params.id}, function(err, user){
res.send(user);
})
})
这是架构:
student.js
const mongoose = require('mongoose');
let StudentSchema = mongoose.Schema({
name:{
type: String
},
email:{
type: String
},
phone:{
type: String
},
password:{
type: String
},
course :[{
type: mongoose.Schema.Types.ObjectId,
ref: 'Course'
}]
}, { collection: 'student' });
const Student = module.exports = mongoose.model('Student', StudentSchema);
course.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let CourseSchema = mongoose.Schema({
student_id:{
type: String
},
course_name:{
type: String
},
course_cost:{
type: String
}
}, { collection: 'course' });
const Course = module.exports = mongoose.model('Course', CourseSchema);
结果:
您需要这样查询:
findOne : to find single document (return object {} )
find : to find multiple documents (return array [])
Student.findOne({_id:req.params.id}, function(err, student){
if(!err && student){
Courses.find({student_id:req.params.id},function(error,courses){
if(!error && courses){
student.courses=courses;
}else{
student.courses=[];
}
res.send(student);
});
}
});
目前您正在获取 course :[]
,因为在学生集合中找不到字段,如您在照片 1 中所见
在学生集合中插入文档时需要设置 course :["coures_id1","coures_id2"]
。
然后您可以使用mongoose populate将课程填充到学生中
Student.findOne({_id:req.params.id}).populate('course').exec(function(err,student){
res.send(student);
});
因此,无需存储 student_id
课程集合中的字段,就像您从学生集合中获取的那样。