feat: UserEquity创建/删除/批量创建后自动更新User.platformCount;新增修复脚本
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
const mongoose = require('mongoose');
|
||||
const User = require('../src/models/User');
|
||||
const UserEquity = require('../src/models/UserEquity');
|
||||
|
||||
async function fixPlatformCount() {
|
||||
try {
|
||||
await mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/member-assistant');
|
||||
console.log('MongoDB connected');
|
||||
|
||||
const users = await User.find({}).select('_id');
|
||||
console.log(`Found ${users.length} users`);
|
||||
|
||||
let updated = 0;
|
||||
for (const user of users) {
|
||||
const count = await UserEquity.countDocuments({ owner: user._id });
|
||||
await User.findByIdAndUpdate(user._id, { platformCount: count });
|
||||
updated++;
|
||||
if (updated % 100 === 0) {
|
||||
console.log(`Progress: ${updated}/${users.length}`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Done! Updated ${updated} users.`);
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fixPlatformCount();
|
||||
Reference in New Issue
Block a user