![](/img/trans.png)
[英]Is there a way to update multiple documents with different values in mongoose?
[英]MongoDb mongoose aggregatePagination returns multiple same documents over different pages
我正面臨着 mongodb 和 mongoose 的分頁問題。 我正在嘗試查詢一組導師,並在分頁和排序(按更新日期)結果中獲取與查詢匹配的導師。 但事實是我在不同的頁面上得到多個相同的文檔......我希望它返回一組獨特的文檔。 這是我的功能的一部分(其余部分只是從請求正文構建查詢):
exports.search = (req, res) => {
var options = { page: page, limit: perPage, sortBy: { updatedDate: -1 }}
const aggregate = Tutor.aggregate([
{
"$geoNear":
{
"near":
{
"type": "Point",
"coordinates": [lon1, lat1]
},
"distanceField": "distance",
"spherical": true,
"maxDistance": radius
}
},
{
$match: match
}
]);
Tutor
.aggregatePaginate(aggregate, options, function (err, result, pageCount, count) {
if (err) {
return res.status(400).send(err);
}
else {
var opts = [
{ path: 'levels', select: 'name' },
{ path: 'subjects', select: 'name' },
{ path: 'assos', select: 'name' }
];
Tutor
.populate(result, opts)
.then(result2 => {
return res.send({
page: page,
perPage: perPage,
pageCount: pageCount,
documentCount: count,
tutors: result2
});
})
.catch(err => {
return res.status(400).send(err);
});
}
})
};
現在,假設我正在查詢第 1 頁,每頁限制為 8。我已經正確取回了我的 8 個文檔,所有文檔都不同。 但是當我查詢第 2 頁時,每頁具有相同的限制,一半的文檔已經在第 1 頁中返回! 你知道為什么會這樣嗎? 謝謝 !
編輯
我認為是sortBy讓它發生了。 如果我刪除它,一切正常。 雖然我需要它...
經過大量研究。 我找到了這個解決方案。 希望它能解決你的問題。
Jobs.aggregatePaginate(aggregateQuery, { page: 1, limit: 10, sort: { 'createdAt': 'desc' } }, function (){});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.