简体   繁体   中英

MongoDB findOneAndUpdate write conflicts significantly affect performance

I have a performance issue with MongoDB.

Our database has loose connection between image and hashtag . When the server saves a new image, it will update imageCount value from hashtag . 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. The problem was that I just deleted line from the model, but the data was still in the DB. Dropping video list from DB fixes the problem.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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