mongoose populate array using skip and limit [duplicate]












0
















This question already has an answer here:




  • mongoDB array pagination

    1 answer



  • Mongoose populate virtual with sort and limit

    1 answer




Model is



const mongoose = require('mongoose')
const Schema = mongoose.Schema

var UserActivitySchema = new Schema({
author: {type: mongoose.Schema.Types.ObjectId, required: true, unique: true, ref: 'user'},
readHistory: [{articleid: {type: mongoose.Schema.Types.ObjectId, ref: 'article'}, date_read: {type: Date, default: Date.now()}}],
watchHistory: [{videoid: {type: mongoose.Schema.Types.ObjectId, ref: 'video'}, date_watch: {type: Date, default: Date.now()}}],
searchHistory: [{query: String, date:{type: Date, default: new Date()}}]
})

module.exports = mongoose.model('useractivity',UserActivitySchema, 'useractivities')


Here i want to fetch readHistory for which i have used query



   if (req.query.page) var page = parseInt(req.query.page);
else var page = 1;
UserActivity.findOne({author: req.client.userId}).select('readHistory')
.populate({path: 'readHistory.articleid', select: 'title description views thumbnailURI', options: { skip: (page-1)*10, limit: 10}, populate: {path: 'author', select: 'displayName -_id'}})
.exec().then(activity=>{
if (!activity || activity.readHistory.length <= 0)
res.json({success: false, message: 'You have not read any article yet.'})
else {
res.json({success: true, readHistory: activity.readHistory})
}
}).catch(err=>{
res.json({success: false, message: err.message})
})


but it returns all the document in the array.
How fix this?










share|improve this question













marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 20:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • It's not the populate() that needs the "limit" here, but the "array". You instead $slice to project a "page" of items findOne({author: req.client.userId}).select({ 'readHistory': { '$slice': [ (page-1)*10, 10 ] }).populate(...) with everything except the options since the $slice is already there. The alternate case is about actually "limiting joins", which is a little more involved and not actually what populate() does, since it's not a "join" but another query retrieving data.

    – Neil Lunn
    Nov 19 '18 at 20:16











  • Missed a closing } in the select() of that example, but you should get the idea. Note also that the terms "skip" and "limit" are even used in the $slice documentation, since this was the original purpose of the operator way back in the original MongoDB design.

    – Neil Lunn
    Nov 19 '18 at 20:29
















0
















This question already has an answer here:




  • mongoDB array pagination

    1 answer



  • Mongoose populate virtual with sort and limit

    1 answer




Model is



const mongoose = require('mongoose')
const Schema = mongoose.Schema

var UserActivitySchema = new Schema({
author: {type: mongoose.Schema.Types.ObjectId, required: true, unique: true, ref: 'user'},
readHistory: [{articleid: {type: mongoose.Schema.Types.ObjectId, ref: 'article'}, date_read: {type: Date, default: Date.now()}}],
watchHistory: [{videoid: {type: mongoose.Schema.Types.ObjectId, ref: 'video'}, date_watch: {type: Date, default: Date.now()}}],
searchHistory: [{query: String, date:{type: Date, default: new Date()}}]
})

module.exports = mongoose.model('useractivity',UserActivitySchema, 'useractivities')


Here i want to fetch readHistory for which i have used query



   if (req.query.page) var page = parseInt(req.query.page);
else var page = 1;
UserActivity.findOne({author: req.client.userId}).select('readHistory')
.populate({path: 'readHistory.articleid', select: 'title description views thumbnailURI', options: { skip: (page-1)*10, limit: 10}, populate: {path: 'author', select: 'displayName -_id'}})
.exec().then(activity=>{
if (!activity || activity.readHistory.length <= 0)
res.json({success: false, message: 'You have not read any article yet.'})
else {
res.json({success: true, readHistory: activity.readHistory})
}
}).catch(err=>{
res.json({success: false, message: err.message})
})


but it returns all the document in the array.
How fix this?










share|improve this question













marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 20:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • It's not the populate() that needs the "limit" here, but the "array". You instead $slice to project a "page" of items findOne({author: req.client.userId}).select({ 'readHistory': { '$slice': [ (page-1)*10, 10 ] }).populate(...) with everything except the options since the $slice is already there. The alternate case is about actually "limiting joins", which is a little more involved and not actually what populate() does, since it's not a "join" but another query retrieving data.

    – Neil Lunn
    Nov 19 '18 at 20:16











  • Missed a closing } in the select() of that example, but you should get the idea. Note also that the terms "skip" and "limit" are even used in the $slice documentation, since this was the original purpose of the operator way back in the original MongoDB design.

    – Neil Lunn
    Nov 19 '18 at 20:29














0












0








0









This question already has an answer here:




  • mongoDB array pagination

    1 answer



  • Mongoose populate virtual with sort and limit

    1 answer




Model is



const mongoose = require('mongoose')
const Schema = mongoose.Schema

var UserActivitySchema = new Schema({
author: {type: mongoose.Schema.Types.ObjectId, required: true, unique: true, ref: 'user'},
readHistory: [{articleid: {type: mongoose.Schema.Types.ObjectId, ref: 'article'}, date_read: {type: Date, default: Date.now()}}],
watchHistory: [{videoid: {type: mongoose.Schema.Types.ObjectId, ref: 'video'}, date_watch: {type: Date, default: Date.now()}}],
searchHistory: [{query: String, date:{type: Date, default: new Date()}}]
})

module.exports = mongoose.model('useractivity',UserActivitySchema, 'useractivities')


Here i want to fetch readHistory for which i have used query



   if (req.query.page) var page = parseInt(req.query.page);
else var page = 1;
UserActivity.findOne({author: req.client.userId}).select('readHistory')
.populate({path: 'readHistory.articleid', select: 'title description views thumbnailURI', options: { skip: (page-1)*10, limit: 10}, populate: {path: 'author', select: 'displayName -_id'}})
.exec().then(activity=>{
if (!activity || activity.readHistory.length <= 0)
res.json({success: false, message: 'You have not read any article yet.'})
else {
res.json({success: true, readHistory: activity.readHistory})
}
}).catch(err=>{
res.json({success: false, message: err.message})
})


but it returns all the document in the array.
How fix this?










share|improve this question















This question already has an answer here:




  • mongoDB array pagination

    1 answer



  • Mongoose populate virtual with sort and limit

    1 answer




Model is



const mongoose = require('mongoose')
const Schema = mongoose.Schema

var UserActivitySchema = new Schema({
author: {type: mongoose.Schema.Types.ObjectId, required: true, unique: true, ref: 'user'},
readHistory: [{articleid: {type: mongoose.Schema.Types.ObjectId, ref: 'article'}, date_read: {type: Date, default: Date.now()}}],
watchHistory: [{videoid: {type: mongoose.Schema.Types.ObjectId, ref: 'video'}, date_watch: {type: Date, default: Date.now()}}],
searchHistory: [{query: String, date:{type: Date, default: new Date()}}]
})

module.exports = mongoose.model('useractivity',UserActivitySchema, 'useractivities')


Here i want to fetch readHistory for which i have used query



   if (req.query.page) var page = parseInt(req.query.page);
else var page = 1;
UserActivity.findOne({author: req.client.userId}).select('readHistory')
.populate({path: 'readHistory.articleid', select: 'title description views thumbnailURI', options: { skip: (page-1)*10, limit: 10}, populate: {path: 'author', select: 'displayName -_id'}})
.exec().then(activity=>{
if (!activity || activity.readHistory.length <= 0)
res.json({success: false, message: 'You have not read any article yet.'})
else {
res.json({success: true, readHistory: activity.readHistory})
}
}).catch(err=>{
res.json({success: false, message: err.message})
})


but it returns all the document in the array.
How fix this?





This question already has an answer here:




  • mongoDB array pagination

    1 answer



  • Mongoose populate virtual with sort and limit

    1 answer








node.js mongodb mongoose






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 16:30









auedbakiauedbaki

2516




2516




marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 20:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 20:10


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • It's not the populate() that needs the "limit" here, but the "array". You instead $slice to project a "page" of items findOne({author: req.client.userId}).select({ 'readHistory': { '$slice': [ (page-1)*10, 10 ] }).populate(...) with everything except the options since the $slice is already there. The alternate case is about actually "limiting joins", which is a little more involved and not actually what populate() does, since it's not a "join" but another query retrieving data.

    – Neil Lunn
    Nov 19 '18 at 20:16











  • Missed a closing } in the select() of that example, but you should get the idea. Note also that the terms "skip" and "limit" are even used in the $slice documentation, since this was the original purpose of the operator way back in the original MongoDB design.

    – Neil Lunn
    Nov 19 '18 at 20:29



















  • It's not the populate() that needs the "limit" here, but the "array". You instead $slice to project a "page" of items findOne({author: req.client.userId}).select({ 'readHistory': { '$slice': [ (page-1)*10, 10 ] }).populate(...) with everything except the options since the $slice is already there. The alternate case is about actually "limiting joins", which is a little more involved and not actually what populate() does, since it's not a "join" but another query retrieving data.

    – Neil Lunn
    Nov 19 '18 at 20:16











  • Missed a closing } in the select() of that example, but you should get the idea. Note also that the terms "skip" and "limit" are even used in the $slice documentation, since this was the original purpose of the operator way back in the original MongoDB design.

    – Neil Lunn
    Nov 19 '18 at 20:29

















It's not the populate() that needs the "limit" here, but the "array". You instead $slice to project a "page" of items findOne({author: req.client.userId}).select({ 'readHistory': { '$slice': [ (page-1)*10, 10 ] }).populate(...) with everything except the options since the $slice is already there. The alternate case is about actually "limiting joins", which is a little more involved and not actually what populate() does, since it's not a "join" but another query retrieving data.

– Neil Lunn
Nov 19 '18 at 20:16





It's not the populate() that needs the "limit" here, but the "array". You instead $slice to project a "page" of items findOne({author: req.client.userId}).select({ 'readHistory': { '$slice': [ (page-1)*10, 10 ] }).populate(...) with everything except the options since the $slice is already there. The alternate case is about actually "limiting joins", which is a little more involved and not actually what populate() does, since it's not a "join" but another query retrieving data.

– Neil Lunn
Nov 19 '18 at 20:16













Missed a closing } in the select() of that example, but you should get the idea. Note also that the terms "skip" and "limit" are even used in the $slice documentation, since this was the original purpose of the operator way back in the original MongoDB design.

– Neil Lunn
Nov 19 '18 at 20:29





Missed a closing } in the select() of that example, but you should get the idea. Note also that the terms "skip" and "limit" are even used in the $slice documentation, since this was the original purpose of the operator way back in the original MongoDB design.

– Neil Lunn
Nov 19 '18 at 20:29












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?