//分页获取数据接口
async function _getAnsCard_page(query, fieldGet, pageSize = 30, afterTime = 0,firstTime = 0) {
const dbcmd = db.command;
var defaultQuery ={startTime: dbcmd.lt(afterTime)}
//若不过滤起始时间
if(afterTime==0&&firstTime==0){
afterTime = new Date().getTime();
defaultQuery.startTime = dbcmd.lt(afterTime);
}//过滤起始时间
else if(afterTime>0 && firstTime>0){
defaultQuery.startTime = dbcmd.gte(firstTime).and(dbcmd.lt(afterTime))
}
var newQuery =Object.assign(
defaultQuery,
query
)
var newFieldGet =Object.assign(
{
_id:true ,
},
fieldGet
)
console.log("newQuery")
console.log(`afterTime=${afterTime},firstTime=${firstTime}`)
let resdb = await examdb_ansCard.where(newQuery).limit(pageSize).orderBy("startTime","desc").get();
return {
errCode: 0,
errMsg: '查询成功',
data: resdb.data,
pageSize:pageSize
}
}
async function _getAnsCard_all(query, fieldGet,afterTime,firstTime){
var haveMore= false;
var allAnsCardList = [];
var pageSize = 500;
do{
console.log("开始获取所有数据query")
var res =await _getAnsCard_page(query,fieldGet,pageSize,afterTime,firstTime);
console.log("获取到数据")
if(res.data.length>0){
allAnsCardList = allAnsCardList.concat(res.data);
}
console.log("获取到数据")
//还有下一页,则继续查询
if(res.data.length==pageSize){
haveMore = true;
var lastItem = res.data[pageSize-1];
afterTime = lastItem.startTime;
}
else {
haveMore = false;
}
console.log("还有下一页"+haveMore)
}while(haveMore);
return allAnsCardList;
}
依山尽 (作者)
的确是,多谢,调成最大后可以了。
2023-04-03 18:49