数据库集合:sp,数据内容如下:
[
{
"_id": "86012367-54b9-4dab-b4bd-d213ef9fe81b",
"lx": 0,
"dt": "2020-03-07T03:09:36.390Z",
"cap": "国家体育局"
},
{
"_id": "981cc2f0-ad33-4275-bb1b-051faddd15bb",
"dt": "2020-03-07T03:04:29.929Z",
"cap": "国家要求",
"lx": 0
},
{
"_id": "491de141-a7e9-49a5-95af-0b0ae3928992",
"lx": 1,
"dt": "2020-03-07T03:11:15.142Z",
"cap": "单杠"
},
{
"_id": "1c856c60-b126-4984-8c86-e6288adae733",
"cap": "太极推手",
"lx": 1,
"dt": "2020-03-07T03:10:47.703Z"
},
{
"_id": "2a9cde70-ca73-4b2e-ab03-13252fb854e3",
"cap": "儿童秋千",
"lx": 1,
"dt": "2020-03-07T02:39:09.084Z"
},
{
"_id": "0aed39ac-49df-4616-a1b5-bf54835151f5",
"cap": "跑步机",
"lx": 1,
"dt": "2020-03-07T02:36:14.685Z"
}
]
代码操作内容:云函数对数据进行聚合排序返回结果,两种方式:云端调用和本地调用,但排序结果不一致。调用结果和代码如下
云端调用的结果不正确,但如果将云端代码中sortdata参数内容直接写死到云函数代码中,执行结果是正确的。
调用代码:
getdatasortcloud("sp",{},{lx:1,dt:-1},0).then(res=>{
console.log('cloud',res)
})
模块函数
module.exports.getdatasortcloud = function getdatasortcloud(tablename, sqldata, sortdata, pagenum) {
if (pagenum < 0) { pagenum = 0 }
return new Promise((resolve, reject) => {
wx.cloud.callFunction(
{
name: "getdatasort",
data: { tablename: tablename, sqldata: sqldata, sortdata: sortdata, pagenum: pagenum },
success: res => {
resolve(res.result.data)
},
fail: err => {
reject()
}
}
)
}
)
}
云函数getdatasort代码:
exports.main = async (event, context) => {
const db = cloud.database()
const tablename=event.tablename
const sqldata=event.sqldata
const sortdata=event.sortdata
const pagenum=event.pagenum
var data=[]
try{
let rs=await db.collection(tablename).aggregate().match(
sqldata
).sort(
sortdata
).skip(pagenum*100).limit(100).end()
data=rs.list
}catch(e){}
return {
data:data,
}
}