我在项目mongodb和postgresql中使用了2个数据库。我必须将存储在pg中的数据附加到基于用户ID的mongodb返回的Json数据中,所以请在这里帮助我。
预期结果:
{
"previous": [
{
"_id": "591453340a59301b9471e3d1",
"university_id": "7",
"email": "time@time.com",
"total_hours": "02:00",
"universityName":"AAAAA"
},
"previous": [
{
"_id": "591453340a59301b9471e3d1",
"university_id": "6",
"email": "time@time.com",
"total_hours": "02:00",
"universityName":"CCCC"
}
]
}
_id
,university_id
,email
&安培; total_hours
来自mongo,universityName
来自PG
来自我的代码
分别考虑mongo json数据和universityName(university_id
在两个数据中都很常见)但没有根据university_id
这是我的代码
entries.find({
"email": data.email,
}).toArray().then(function(result) {
result.map(function(t){
db.query('select * from universities where _id =$1', [t.university_id],function(err,univers){
if (err) return callback(new Error('error'));
result.universityName = univers.rows[0].universityName
return callback(null, result);
})
})
}).catch(function(err) {
return callback(new Error(err));
})
Mongodb database
{
"_id": "591453340a59301b9471e3d1",
"university_id": "7",
"email": "time@time.com",
"total_hours": "02:00"
}
Postgresql:
_id | universityName
1 | aaaa
6 | bbbb
7 | vvvv