Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

react-native mongodb set, push data into array of array that is consists of arrays both

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...