例如:同一个集合中随机A类型10条,B类型取20条,C类型取10条
async function getRandomList(event){
const queryResult = await db.collection(‘subject1’)
.aggregate()
.match({
typeText: ‘判断’
})
.sample({
size:3
})
.end()
console.log(queryResult);
const {list, errMsg} = queryResult;
if (errMsg == “collection.aggregate:ok”){
return {
errCode:0,
errMsg:errMsg,
questionList:list,
}
}else{
return {
errCode:1,
errMsg:errMsg,
}
}
}
/**
* 随机查询题目列表
* @param {object} event
*/
async function getRandomList1(event){
const queryResult = await db.collection(‘subject1’)
.aggregate()
.match({
typeText: ‘单选’
})
.sample({
size:1
})
.end()
console.log(queryResult);
const {list, errMsg} = queryResult;
if (errMsg == “collection.aggregate:ok”){
//查询成功返回的数据
return {
errCode:0,
errMsg:errMsg,
questionList:list,
}
}else{
//查询失败返回的数据
return {
errCode:1,
errMsg:errMsg,
}
}
}
/**
* 随机查询题目列表
* @param {object} event
*/
async function getRandomList2(event){
const queryResult = await db.collection(‘subject1’)
.aggregate()
.match({
typeText: ‘判断’
})
.sample({
size:2
})
.end()
console.log(queryResult);
const {list, errMsg} = queryResult;
if (errMsg == “collection.aggregate:ok”){
//查询成功返回的数据
return {
errCode:0,
errMsg:errMsg,
questionList:list,
}
}else{
//查询失败返回的数据
return {
errCode:1,
errMsg:errMsg,
}
}
}
// 查询数据库集合云函数入口函数
exports.main = async (event, context) => {
// 返回数据库查询结果
return getRandomList(event);
};