optimisation for backlink deleting, use an with updadtemany instead of updateOne to reduce bulkwrite amount instead of one for everyt single quote (duhhh)

merge-requests/208/head
fatchan 5 years ago
parent 1932e597ad
commit fcec878053
  1. 30
      models/forms/deletepost.js
  2. 1
      models/forms/makepost.js

@ -50,28 +50,26 @@ module.exports = async (posts, board) => {
return acc; return acc;
}, { postFiles: [], postBacklinks: [], postMongoIds: [] }); }, { postFiles: [], postBacklinks: [], postMongoIds: [] });
//is there a nicer way to do this
const bulkWrites = []; const bulkWrites = [];
for (let j = 0; j < allPosts.length; j++) { for (let j = 0; j < allPosts.length; j++) {
const post = allPosts[j]; const post = allPosts[j];
for (let i = 0; i < post.quotes.length; i++) { //remove the backlink to this post from any post that it quoted
const quote = post.quotes[i]; bulkWrites.push({
//remove the backlink to this post from any post that it quoted 'updateMany': {
bulkWrites.push({ 'filter': {
'updateOne': { '_id': {
'filter': { '$in': post.quotes.map(q => q._id)
'_id': quote._id }
}, },
'update': { 'update': {
'$pull': { '$pull': {
'backlinks': { 'backlinks': {
'postId': post.postId 'postId': post.postId
}
} }
} }
} }
}); }
} });
} }
if (bulkWrites.length > 0) { if (bulkWrites.length > 0) {
await Posts.db.bulkWrite(bulkWrites); await Posts.db.bulkWrite(bulkWrites);

@ -272,7 +272,6 @@ module.exports = async (req, res, next) => {
//for cyclic threads, delete posts beyond bump limit //for cyclic threads, delete posts beyond bump limit
if (thread && thread.cyclic && thread.replyposts > res.locals.board.settings.replyLimit) { if (thread && thread.cyclic && thread.replyposts > res.locals.board.settings.replyLimit) {
//is there a way to NOT have to fetch before deleting for this?
const cyclicOverflowPosts = await Posts.db.find({ const cyclicOverflowPosts = await Posts.db.find({
'thread': data.thread, 'thread': data.thread,
'board': req.params.board 'board': req.params.board

Loading…
Cancel
Save