I'm using nodejs with mongodb.
Our team is currently building an simple android community application.
As I am the server programmer, I want to know are there any better ways handling the database with mongodb.
The picture above shows our application.
When the user click an article(left image) then application move to the article page with content of the article(① mark of the right image) and comments of the article(② mark of the right image)
We successfully created those application.
But had serious problem : slow response time
The application bring the data through each two POST API.
One is /selectBoardArticle which loads content of the article and the other is /selectCommentList which loads whole comments of the article.
It takes to request to bring the data of an article.
Some developer said two request cause the extremely response time. So I'm trying to integrate /selectBoardArticle and /selectCommentList API.
I know two options to integrate data of two collection.
First, using db.collection.aggregate function.
This is the source code of /selectBoardArticle API
app.post('/selectBoardArticle', util.session, function (req, res) {
TN_BBS.find({bbs_id: req.body.bbs_id, ntt_id: req.body.ntt_id, use_at: "Y"}).exec(function (err, result) {
if (result.length) {
//increase read count and save
result[0].rdcnt++;
result[0].save(function () {
//join comment(TN_COMMENT) and image(TN_FILEDETAIL) data of the article
TN_BBS.aggregate([{
"$lookup": {
from: "TN_COMMENT",
localField: "ntt_id",
foreignField: "ntt_id",
as: "COMMENT"
}
}, {
"$lookup": {
from: "TN_FILEDETAIL",
localField: "atch_file_id",
foreignField: "atch_file_id",
as: "TN_FILEDETAIL"
}
}, {
"$match": {
ntt_id: req.body.ntt_id,
bbs_id: req.body.bbs_id
}
}]).limit(1).exec(function (err, result) {
if (err) {
return res.send(err);
}
res.end(JSON.stringify(result[0]));
setPopPoint(req.body.ntt_id);
})
});
}
else
res.end("no result!");
});
Second, find each article and comment data and insert comment data into article data through javascript
router.get('/selectBoardArticles/:bbs_id', function (req, res) {
var query = {bbs_id: req.params.bbs_id, use_at: "Y"};
TN_BBS.find(query, function (err, doc) {
if (err) {
return res.send(err);
}
var ntt_ids = [];
for (var i = 0; i < doc.length; i++) {
ntt_ids.push(doc[i].ntt_id);
}
TN_COMMENT.find({bbs_id: req.params.bbs_id, ntt_id: {$in: ntt_ids}}, function (err, doc2) {
for (var i = 0; i < doc.length; i++) {
doc[i].comment = [];
for (var j = 0; j < doc2.length; j++)
if (doc2[j].ntt_id == doc[i].ntt_id) doc[i].comment.push(doc2[j]);
}
res.render('admin/board_articles', {data: doc, length: doc.length, boardMaster: req.params.bbs_id});
});
});
Though this API returns article list the concept I want to say is find twice and inserting child data through javascript.
I found few problems dealing with joining collections.
First problem
I feel like using mongodb as a relational database which is not recommended.
The idea behind MongoDB is to eliminate (or at least minimize) relational data. https://stackoverflow.com/a/8129650/5593805
If I understand correctly I should avoid joining collections.
But I could not find the way to avoid joining collections.
Should I integrate two collections into one collection?
When someone frequently use aggregation in mongodb I think it is better off using relational database.
I want to know how do you think about this.
Second problem
To join two collections in mongodb.
I have to write really complicated code, compared to relational database.
More specifically, mongodb needs really long and complicated join query which can be done easily and intuitively in mysql with JOIN query, cause bad for maintenance.
I want to know are there any better and effective ways joining two collections in mongoDB in nodejs?