最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

数据库迁移mongo db express节点的问题

网站源码admin31浏览0评论

数据库迁移mongo db express节点的问题

数据库迁移mongo db express节点的问题

我最近将新字段添加到我的用户模型中,如下所示

{fingerprint: { type: String, index: true }

现在我想做数据库迁移,这样我数据库中的旧用户也可以拥有指纹。以下是我如何努力实现这一目标

const updateFingerprintsForExistingUsers = async () => {
try {
const batchSize = 5; 
const users = await User.find({ fingerprint: { $exists: false } })
  .select("+fingerprint")
  .batchSize(batchSize)
  .maxTimeMS(100000);
console.log(users);
for (const user of users) {
  const fingerprintData = generateFingerprint({
    headers: {
      "user-agent": user.userAgent,
    },
    connection: {
      remoteAddress: user.clientIP, 
    },
  });

  
  user.fingerprint = fingerprintData.fingerprint;
  await user.save(); 
}
console.log("Fingerprints updated for all existing users.");
} catch (error) {
console.error("Error updating fingerprints:", error);
}
};

但我不断收到此错误

Error updating fingerprints: MongooseError: Operation `users.find()` buffering timed out after 10000ms
at Timeout.<anonymous> (C:\Users\cross\Desktop\educ\educompanion\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:198:23)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)

我已经在代码上尝试了几件事,正如你在上面看到的那样

  • 使用批量大小
  • 增加最大超时时间
回答如下:
发布评论

评论列表(0)

  1. 暂无评论