问题描述
我正在使用nodejs获取mongodb的 两个 数据形式。一个是 学生 集合,另一个是是 书 ...在这个项目中,我假设这是 一对多 关系(一个学生可以可以阅读多本书,但每本书只能由一名学生阅读。.这可能是多对多的关系,但我会根据情况而定(一个学生可以一次阅读多本书,但每本书只能由一名学生阅读。现在一次。.现在通过在(node js)中使用 populate 函数,我正在获取 books 集合和学生的所有列数据使用Book模型进行馆藏,因为我在书籍中使用了学生模型的参考,但是当我在所有列上应用数据表服务器端正则表达式搜索时,它只会过滤 books 馆藏的列,而不会列的工作属于 学生 集合。 ** 我正在前端(客户端)使用jquery和jquery数据表 **
const mongoose = require('mongoose');
const BookSchema = mongoose.Schema({
student:{
type:mongoose.Schema.Types.ObjectId,ref: 'Student'
},title: String,code: String,edition:String
});
module.exports = mongoose.model('Book',BookSchema,'books');
const mongoose = require('mongoose'); // following is a student model (schema)
const StudentSchema = mongoose.Schema({
name: String,rollnumber: String,class: String,});
module.exports = mongoose.model('Student',StudentSchema,'students');
app.post("/testrelations",function(req,res) {
var query = req.body.search.value;
if(req.body.search.value) {
regex = new RegExp(req.body.search.value,"i")
query = { $or: [{'title': regex},{'code': regex },{'edition': regex},{'student.rollnumber': regex},// this is not filtering
] };
}
else{
query = {};
}
Book.count(query).populate("student").exec(function(err,user) {
recordsTotal = user;
Book.count(query,function (err,c) {
recordsFiltered = c;
console.log(req.body.start);
console.log(req.body.length);
Book.find(query).populate("student").exec(function(err,results) {
if (!results) {
console.log('error while getting results' + err);
return;
}
var get_columns= [];
var obj = {};
for (var i = 0; i < results.length; i++) {
var obj = {
_id: results[i]._id,code: results[i].code,title: results[i].title,student_name:results[i].student.name,roll_num:results[i].student.rollnumber
}
get_columns.push(obj);
}
var data = JSON.stringify({
"draw": req.body.draw,"recordsFiltered": recordsFiltered,"recordsTotal": recordsTotal,"data": get_columns
});
res.send(data);
});
})
})
})
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)