2016-03-22 127 views
0

當用戶保存一個問題時,問題可以包含一個「標籤」數組。我想獲取該數組標籤並檢查它們是否存在於標籤集合中。如果標籤存在,更新計數,如果沒有,則將其添加到集合中。我有寫這樣做的代碼,但它看起來很冗長。用mongo/mongoose做更好/更簡單/更簡潔的方法嗎?該功能類似於堆棧溢出如何處理標記。MongoDB 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"}); 
       }) 
      } 
     }) 
    } 
}); 

回答

1

可以使用find()stream()從貓鼬API時使用MongoDB的$in運算符表達式。 find()返回一個Query對象,然後該對象可用於創建一個QueryStream(實現Node.js接口)。然後您可以使用.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"}); 
       }); 
      } 
     }); 
    }); 
+0

這並不完全正常...... QuestionTags.find內的所有內容不再適用,因爲它不在forEach中。如果'tagsRegExp'包含6個標籤,並且其中5個標籤存在於集合中,那麼您將在查詢響應中找回這5個標籤。這對於爲不存在的東西創建新標籤沒有幫助。 – erichardson30

+0

@ erichardson30我更新了從'find()'返回的'Query'對象上使用Mongoose QueryStream的代碼和解釋。 – roflmyeggo

+0

這不會保存任何問題到questionTags集合 – erichardson30