简体   繁体   English

MongoDB findOneAndUpdate 写入冲突显着影响性能

[英]MongoDB findOneAndUpdate write conflicts significantly affect performance

I have a performance issue with MongoDB.我遇到 MongoDB 的性能问题。

Our database has loose connection between image and hashtag .我们的数据库在imagehashtag之间有松散的联系。 When the server saves a new image, it will update imageCount value from hashtag .当服务器保存新图像时,它会更新hashtag中的imageCount值。 The problem is that there can be hundreds of updates on same hashtag at the same time.问题是同一主题标签上可能同时有数百个更新。 Saving is very slow because of write conflicts.由于写入冲突,保存速度非常慢。

Have I misunderstood how such a connection should be made?我是否误解了应该如何建立这种联系? Is it possible to make this work faster?是否有可能使这项工作更快?

Thanks!谢谢!

Current approach update hashtag query:当前方法更新主题标签查询:

const dbHashtag = await Hastag.findOneAndUpdate(
  { name: hashtag.name },
  { $inc: { videoCount: 1 } },
  { upsert: true, new: true }
).select('_id').lean()

Image schema:图像架构:

const imageSchema = new mongoose.Schema({
  hashtags: [{ type: Schema.Types.ObjectId, ref: 'Hashtags' }]
})

Hashtag schema:标签架构:

const hashtagSchema = new mongoose.Schema({
  name: {
    type: 'String',
    unique: true
  },
  imageCount: {
    type: 'Number'
  },
})

hashtagSchema.index({ name: 1 })

This was my mistake.这是我的错误。 Earlier I have many to many relationships between image and hashtag.早些时候,我在图像和主题标签之间建立了多对多关系。

const hashtagSchema = new mongoose.Schema({
  name: {
    type: 'String',
    unique: true
  },
  imageCount: {
    type: 'Number'
  },
  images: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Image"
    }
  ]
})

I dropped images list from tags, because it may be over 100000 ids.我从标签中删除了图像列表,因为它可能超过 100000 个 ID。 The problem was that I just deleted line from the model, but the data was still in the DB.问题是我刚刚从 model 中删除了行,但数据仍在数据库中。 Dropping video list from DB fixes the problem.从数据库中删除视频列表可以解决问题。

await Hastag.updateMany({}, { $unset: { images: 1 } })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM