i'm bigginer of react-native and mongodb.
i'm making some board.
so, i want to treat some data, $push, $set, or $pull when i delete, create contents, or comments.
but, i don't know how to control this.
my mongo db collections are consists of like this.
board:{
name: 'hiphop'
introduction: 'hiphop lovers',
time: '2021/01/22',
contents: [
{likes:3,
commentsNum: 4,
title:'Hi',
contents:'nice to meet u',
comments: [
{likes:2,
content: 'zz',
},
{likes:3,
content: 'hello',
}
],
{likes:3,
commentsNum: 4,
title:'Hi',
contents:'zz',
comments: [
{likes:5,
content: 'good',
},
],
}
router.post('/createComment', async (req, res) => {
const { userName, userEmail, comment, time, boardName, contentId, commentsNum } = req.body;
const newComment = await Comment({ userName, userEmail, comment, time });
try {
const currentBoard = await Board.findOneAndUpdate({ 'name': boardName, 'contents._id': contentId }, {$push: {'contents.$.comments': newComment}, $set: {'contents.$.commentsNum': commentsNum+1 }}, {new:true});
res.send([currentBoard, currentBoard.contents[???]]);
} catch (err) {
return res.status(422).send(err.message);
}
});
router.post('/plusContentLike', async (req, res) => {
const { name, contentId, likes } = req.body;
try {
const currentBoard = await Board.findOneAndUpdate({ 'name': name, 'contents._id': contentId }, {$set: {'contents.$.likes': likes+1}}, {new:true});
res.send([currentBoard, currentBoard.contents[???]]);
} catch (err) {
return res.status(422).send(err.message);
}
});
below two is my code. but i don't know how to get 'currentBoard.contents[???]' that is corresponding founded contents.
i can find them one more server request like const currentContent Board.findOne~~~.
but i think it is not effective way because of two request.
is it okay to request two in programming effectivity??
As a result, how can i access array of array(Board->contents->comments) and how to get object that is found in one request?? help me plz T_T
question from:
https://stackoverflow.com/questions/65869508/react-native-mongodb-set-push-data-into-array-of-array-that-is-consists-of-arra