cb/server/laf-cloud/functions/searchPlayerDataByLevel.ts
2026-07-02 11:22:24 +08:00

57 lines
1.9 KiB
TypeScript

import cloud from '@lafjs/cloud'
const { MongoClient} = require('mongodb');
const db1 = cloud.database();
export default async function (ctx: FunctionContext) {
const uri = 'mongodb://root:nlwrr6cr@dbconn.sealoshzh.site:48953/?directConnection=true';
const dbName = 'sealaf-cno56wxmj7';
// let res = await db1.collection("users").where({ totalLevel: { $gt: 10000 } }).update({ totalLevel:0},{multi:true});
// return res;
let data=[];
// for(let i=0;i<res.data.length;i++){
// data.push(res.data[i].openid);
// }
console.log(data);
return data;
const client = new MongoClient(uri);
try {
await client.connect();
const db = client.db(dbName);
const collections = await db.listCollections().toArray();
const collectionName = 'users';
const exists = collections.some((item) => item.name === collectionName);
if (!exists) {
console.log(`collection "${collectionName}" not found`);
return;
}
for(let i=0;i<3;i++){
console.log(data[i]);
const user = await db.collection(collectionName).findOne({ _id:data[i]});
let data1={
endLevelNum: user.endLevelNum||0,
totalLevel: user.totalLevel||user.levelAmount,
userLevel: user.userLevel||0,
forcedUpdate: true,
}
if (user) {
//data1.coinAmount += 10000;
//console.log(data1);
let res22=await db1.collection("users").where({_id:data[i]}).update(data1);
//console.log(res22)
}
}
} catch (err) {
console.error('MongoDB error:', err);
} finally {
console.log("断开连接")
await client.close();
}
// let res = await db1.collection("users").where({ endLevelNum:99999999999999 }).get();
// let data=[];
// for(let i=0;i<res.data.length;i++){
// if (res.data[i].coinAmount>20000){
// data.push(res.data[i]._id);
// }
// }
// console.log(JSON.stringify(data));
}