mongoose - mongodb 关联多表问题
问题描述
我使用的是express4+mongodb搭建一个API,多表关联已经折磨我好几天了,一直没有悟出其中的关联效果,突然感觉mongodb好垃圾!
有大神能够帮帮我吗?
比如我有两张表user和job,user中有一个自定义的id,job中也有一个同样的id,他们的id值是一样的。
我现在想要通过user表的id获取job的内容!
user表:
{
"_id" : ObjectId("5905690f5ac92b6422e3c914"),
"id" : "1",
"name" : "lizhuang",
"password" : "",
"fullname" : "李章",
"wechat" : "xiao123",
"avatar" : "",
"email" : "xxx@xxx.com",
"tel" : "13123212333"
}
job表
/* 1 */
{
"_id" : ObjectId("5906bfcd5ac92b6422e4dd86"),
"boss_id" : "1",
"job_name" : "前端开发",
"cat" : "技术",
"position" : "深圳南山区",
"years" : "3-5",
"education" : "本科",
"information" : "主要负责XXXXX的手机端web app开发",
"skill" : "HTML5,Angular,Jquery,javascript",
"team_info" : "XXXXX前端团队组建于2002年,目前已有100人"
}
/* 2 */
{
"_id" : ObjectId("5906c04c5ac92b6422e4dec7"),
"boss_id" : "1",
"job_name" : "Web前端",
"cat" : "技术",
"position" : "北京昭阳区",
"yeas" : "3-5",
"education" : "研究生",
"information" : "主要负责xxxxxWEB页面,H5页面开发",
"skill" : "HTML5,javascript",
"team_info" : "xxxxxx前端团队组建于2002年,目前已有100人"
}
/* 3 */
{
"_id" : ObjectId("5906c06f5ac92b6422e4df57"),
"boss_id" : "1",
"job_name" : "APP测试工程师",
"cat" : "技术",
"position" : "深圳南山区",
"yeas" : "3-5",
"education" : "本科",
"information" : "主要负责APP的测试以及文档编写",
"skill" : "HTML5,javascript,IOS,Android",
"team_info" : "xxxxxx测试团队组建于2002年,目前已有100人"
}
我需要的效果就是将job添加到user里面作为子文档,如下效果:
{
"_id" : ObjectId("5905690f5ac92b6422e3c914"),
"id" : "1",
"name" : "lizhuang",
"password" : "",
"fullname" : "李章",
"wechat" : "xiao123",
"avatar" : "",
"email" : "xxx@xxx.com",
"tel" : "13123212333",
"jobs":[ /* 1 */
{
"_id" : ObjectId("5906bfcd5ac92b6422e4dd86"),
"boss_id" : "1",
"job_name" : "前端开发",
"cat" : "技术",
"position" : "深圳南山区",
"years" : "3-5",
"education" : "本科",
"information" : "主要负责XXXXX的手机端web app开发",
"skill" : "HTML5,Angular,Jquery,javascript",
"team_info" : "XXXXX前端团队组建于2002年,目前已有100人"
}
/* 2 */
{
"_id" : ObjectId("5906c04c5ac92b6422e4dec7"),
"boss_id" : "1",
"job_name" : "Web前端",
"cat" : "技术",
"position" : "北京昭阳区",
"yeas" : "3-5",
"education" : "研究生",
"information" : "主要负责xxxxxWEB页面,H5页面开发",
"skill" : "HTML5,javascript",
"team_info" : "xxxxxx前端团队组建于2002年,目前已有100人"
}
/* 3 */
{
"_id" : ObjectId("5906c06f5ac92b6422e4df57"),
"boss_id" : "1",
"job_name" : "APP测试工程师",
"cat" : "技术",
"position" : "深圳南山区",
"yeas" : "3-5",
"education" : "本科",
"information" : "主要负责APP的测试以及文档编写",
"skill" : "HTML5,javascript,IOS,Android",
"team_info" : "xxxxxx测试团队组建于2002年,目前已有100人"
}]
}
根据网上给出的各种方法,我都尝试了,根本无法获取数据,都是空的。另外网上的方法中很多东西说得不明不白的,让我很是无奈。尝试几百次后,一点效果都没有!
比如我的userSchema.js代码如下:
/**
* Created by 深夜徘徊 on 2017/4/20.
*/
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// user数据库数据类型
var bossSchema = new Schema({
_id:Object,
id:String,
name: String,
password: String,
fullname: String,
wechat:String,
avatar:String,
email:String,
tel:String,
jobs:[{
type:String,
ref:'job'
}
]
});
// bind module for accessing outside
module.exports = mongoose.model('User', bossSchema, 'user');
job.js的如下:
/**
* Created by Administrator on 2017/5/1.
*/
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//job 数据类型
var jobSchema = new Schema({
boss_id:{ type: Schema.Types.String, ref: 'Boss' },
job_name: String,
cat: String,
position: String,
years:String,
education:String,
information:String,
skill:String,
team_info:String
})
module.exports = mongoose.model('Job', jobSchema, 'job');
在我看来,他们根本就不可能关联,因为他们之间一点关系都没有,每个数据模型都是一个js,两个js中又无法进行相关连调,所以取到的值永远都是空的。
我的router文件代码如下:
// 获取用户列表
router.get('/', function(req, res){
Boss.findOne({name:'lizhuang'}).populate('job').exec(function(err,docs){
res.json(docs)
})
})
这样一来,取到的永远是空,尝试了不下几百次了,试过多种方法,他就是没有任何作用。大神出来帮帮忙吧!
另外还有一个问题,我之前试过将job表中的内容直接放到user表中,user表最终效果如下:
{
id:....
jobs:[{
......
},
{
......
},
{
......
}]
}
这种模式确实省去了关联的这个步骤,但是问题出现了,我该如何通过模糊查询,仅查job中的相关数据,我也使用$or $regex等方法,如果使用这样的层级json格式进行模糊查询永远都是失败的,或许我的代码写的不对。但是如果把job独立出来直接进行模糊查询,那是可以比较准确的显示的。
模糊查询的代码如下:(此代码仅对于单独的job表)
// 全文多条件搜索职位信息
router.get('/:keyword', function(req, res) {
var keyword = req.params.keyword;
var key = new RegExp(keyword,'i') //模糊查询参数,i不区分大小写
Job.find({$or:[
{'job_name':{$regex:key}},
{'information':{$regex:key}},
{'skill':{$regex:key}},
{'team_info':{$regex:key}}
]},function (err, jobs) {
if (err) {
return res.send(err);
}
res.json(jobs);
})
首先必须要明确,填充是根据 _id
字段进行填充的
抱歉,现在才来补充代码。
我这里简化一下你的内容,这样也便于理解
这里我就展示填充user的jobs
//user集合
{
"_id" : ObjectId("593f849706344f187ae48da9"),
"name" : "user1",
"jobs" : [
"job1",
"job2"
]
}
//job集合
{
"_id" : "job1",
"name" : "job1"
}
{
"_id" : "job2",
"name" : "job2"
}
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = new Schema({
name: String,
jobs:[{type:String,ref:'Job'}]
});
var User = mongoose.model('User',userSchema);
var jobSchema = new Schema({
_id:String,
name: String
})
var Job= mongoose.model('Job',jobSchema)
mongoose.connect('mongodb://localhost:27017/test', function(error){
if(error) console.log('Failed to connect');
else {
User.find().populate('jobs').exec(function(err,docs){
console.log(docs)
//打印结果的jobs: [ [Object], [Object] ] } ]
//这俩就是填充后的job文档了,具体输出便可查看
})
}
});
这篇关于mongoose - mongodb 关联多表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!