2017-03-21 37 views
0

我想限制由Model.find()MongoDB中返回的數據長度的數據/貓鼬限制由model.find()返回在MongoDB中

這裏是我的代碼 想從返回「摘錄」內容。

Blog.find({},{ 
    title: 1, 
    content: 1 // basically wants to return content not more than 200 character 
    }, function(err, data){ 
    if (err) { 
     console.log(err); 
    } 
    res.render('blog/posts', { 
     title:'All posts', 
     posts: data 
    }); 
}); 

換句話說如何從MongoDB的

更新 返回有限的內容中找到解決方案:

Match with substring in mongodb aggregation

+0

你試過用'filter' https://docs.mongodb.com/manual/reference/operator/aggregation/filter/ –

+0

過濾器返回滿足一定條件的文檔,但是我想返回所有文檔,但是分片>>例如。如果文檔有像'這是非常長的文本顯示'的條目,我想只返回'這是非常..' –

+1

[與MongoDB聚合中的子字符匹配]的可能重複(http://stackoverflow.com/questions/20066279/match-with-substring-in-mongodb-aggregation) –

回答

1

你只需要通過極限參數在第三選項

Blog.find({},{ 
     title: 1, 
     content: 1 // basically wants to return content not more than 200 character 
    },{ limit:10 }, function(err, data){ 
    if (err) { 
     // You should handle this err because res.render will send 
     // undefined if you don't. 
     console.log(err); 
    } 
    res.render('blog/posts', { 
     title:'All posts', 
     posts: data 
    }); 
}); 
+0

它限制json對象不是數據長度ie。如果以前它返回100條記錄,現在它只會返回10條記錄......按照此代碼 –

+0

那麼你想要什麼你想要的是它應該返回你100個文件和數據長度應該是10 ?.這背後的邏輯是什麼,請清楚你想要什麼,所以我們可以提供幫助。 –

+0

找到了解決方案,我正在尋找一些像 http://stackoverflow.com/questions/20066279/match-with-substring-in-mongodb-aggregation @ShumiGupta –