MongooseError [ParallelSaveError]:无法并行保存()同一个文档
Posted
技术标签:
【中文标题】MongooseError [ParallelSaveError]:无法并行保存()同一个文档【英文标题】:MongooseError [ParallelSaveError]: Can't save() the same doc multiple times in parallel 【发布时间】:2020-04-11 00:24:44 【问题描述】:我遇到了 mongoose 和 nodejs 的问题。可能是我写了错误的代码或任何其他问题,请帮忙。这是我的控制器文件。 alldata.save
给[ParallelSaveError]
let createData = async function(req,res,next)
let body = req.body;
let alldata = new League(body);
let start_time = new Date().getTime();
try
await Leaguecategories.find()
.then(async function(categories)
categories.forEach(async function(category)
//here i am assigning foreign key
alldata.league_category_id = category._id;
await alldata.save(function(err, book)
if(err)
console.log(err);
else
res.send(status: 0, statusCode:"success", message: "Successfully inserted.")
);
)
)
catch (error)
return res.send(status : 1 , statusCode : "error" , message : error.message)
这是我的 Leaguecategories 模型
var mongoose = require('mongoose');
const league_categories = new mongoose.Schema(
name:
type: String,
required: true
,
active:
type: String,
required: true
,
create_date:
type: Date,
required: true,
default: Date.now
,
league_type_id:
type: String,
required: 'league_type',
required:true
)
module.exports = mongoose.model('Leaguecategories', league_categories)
这是我的联赛模型
var mongoose = require('mongoose');
const league = new mongoose.Schema(
title:
type: String,
required: true
,
pool_price:
type: Number,
required: true
,
entry_fee:
type: Number,
required: true
,
total_spots:
type: Number,
required: true
,
start_time:
type: Date,
required: true
,
end_time:
type: Date,
required: true
,
create_date:
type: Date,
required: true,
default: Date.now
,
active:
type: String,
required: true
,
league_category_id:
type: String,
ref: 'Leaguecategories',
required:true
)
module.exports = mongoose.model('League', league)
【问题讨论】:
【参考方案1】:您必须每次都创建League
的新实例。像这样:
categories.forEach(async function(category)
//here i am assigning foreign key
let alldata = new League(body);
alldata.league_category_id = category._id;
...
);
建议:
您为什么同时使用async/await
和.then()
?您应该只使用其中之一。此外,还有一些其他问题。
await
不能在 forEach
内部工作
每次拨打.save()
时,您都会拨打res.send()
。这也可能最终引发错误。
你可以像这样重构代码。
try
const categories = await Leaguecategories.find();
const promises = categories.map(function (category)
//here i am assigning foreign key
let alldata = new League(body);
alldata.league_category_id = category._id;
return alldata.save();
);
await Promise.all(promises);
res.send( status: 0, statusCode: "success", message: "Successfully inserted." )
catch (error)
return res.send( status: 1, statusCode: "error", message: error.message );
【讨论】:
以上是关于MongooseError [ParallelSaveError]:无法并行保存()同一个文档的主要内容,如果未能解决你的问题,请参考以下文章
MongooseError:操作“featureds.find()”缓冲在 10000 毫秒后超时
MongooseError:操作“featureds.find()”缓冲在 10000 毫秒后超时
MongooseError:操作 `products.insertOne()` 缓冲在 10000 毫秒后超时
MongooseError:操作 `blacklistScema.find()` 缓冲在 10000 毫秒后超时
使用 import 而不是 require() 时带有 uuid 的 MongooseError
MongooseError [CastError]:模型“List”的路径“_id”的值“ name:'C'”转换为 ObjectId 失败