簡體   English   中英

MongoDB Mongoose使用數組保存或更新

[英]MongoDB Mongoose save or update with array

當用戶保存問題時,該問題可以包含“標簽”數組。 我想采用該標簽數組,並檢查它們是否存在於標簽集合中。 如果標簽存在,請更新計數,否則,將其添加到集合中。 我已經編寫了代碼來執行此操作,但是它看起來很冗長。 使用mongo / mongoose是否有更好/更簡便/更簡潔的方法? 該功能有點類似於堆棧溢出對其標記的工作方式。

apiRouter.post('/', function(req, res) {
    var question = new Questions();

    question.text = req.body.text;
    question.answers = req.body.answers;
    question.tech = req.body.tech;
    question.tags = req.body.tags;
    question.level = req.body.level;
    question.createdAt = req.body.createdAt;

    question.save(function(err, questions) {
        if(err) res.send(err);
        res.json({message: "Question was created."});
    });

    for each(tag in req.body.tags) {
        QuestionTags.find({ 'tag': { $regex: new RegExp(tag, "i") } }, function(err, tags) {
            if(err) res.send(err);
            if(tags.length === 0) {
                var tagObj = new QuestionTags();
                tagObj = {
                    tag: tag,
                    count: 0
                }
                tagObj.save(function(err, questions) {
                    if(err) res.send(err);
                    res.json({message: "Tag created"});
                });
            } else {
                var tagObj = new QuestionTags();
                tagObj = tags;

                tagObj.count++;

                tagObj.save(function(err, questions) {
                    if(err) res.send(err);
                    res.json({message: "Tag updated"});
                })
            }
        })
    }
});

當從Mongoose API將find()stream()結合使用時,可以使用MongoDB $ in運算符表達式。 find()返回一個Query對象,然后可以使用該對象創建QueryStream (實現Node.js ReadableStream接口)。 然后,您可以使用.on處理每個流事件。

請注意,您不能在$in表達式內使用$regex運算符表達式,因此在將數組傳遞給find()之前,您必須注意這一點。

apiRouter.post('/', function(req, res) {
    var question = new Questions();

    question.text = req.body.text;
    question.answers = req.body.answers;
    question.tech = req.body.tech;
    question.tags = req.body.tags;
    question.level = req.body.level;
    question.createdAt = req.body.createdAt;

    question.save(function(err, questions) {
        if(err) res.send(err);
        res.json({message: "Question was created."});
    });

    var tagsRegExp = [];

    req.body.tags.forEach(function(tag) {
        tagsRegExp.push(new RegExp(tag, "i");
    }

    QuestionTags.find({ 'tag': { $in : tagsRegExp }}).stream()
        .on('error', function(err) {
            res.send(err);
        }).on('data', function(tag) {
            if(tag.length === 0) {
                var tagObj = new QuestionTags();
                tagObj = {
                    tag: tag,
                    count: 0
                }
                tagObj.save(function(err, questions) {
                    if(err) res.send(err);
                    res.json({message: "Tag created"});
                });
            } else {
                var tagObj = new QuestionTags();
                tagObj = tag;

                tagObj.count++;

                tagObj.save(function(err, questions) {
                    if(err) res.send(err);
                    res.json({message: "Tag updated"});
                });
            }
        });
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM