如何保存文档并更新另一个与猫鼬相互依赖的文档
how to save document and update another which are dependent on each other with mongoose
这是我的出价模型。
const BidSchema = new Schema({
auctionKey: {
type: mongoose.Types.ObjectId,
ref: "Auction",
required: true
},
amount: { type: String, required: true },
userName: { type: String, required: true },
});
还有,这是我的拍卖模型(注意这两个模型之间的关系)。
const AuctionSchema = new Schema({
title: { type: String, required: true },
startDate: { type: Date, required: true },
closeDate: { type: Date, required: true },
initialBidAmount: { type: Number, required: true },
bidIncrementAmount: { type: Number, required: true },
bids: [
{
type: mongoose.Types.ObjectId,
ref: 'Bid'
}
]
});
当用户为任何拍卖出价时,我将出价保存在出价收集中并使用 mongoose 更新拍卖收集 findOneAndUpdate
。
const postBid = async (req, res, next) => {
const { auctionKey } = req.body;
const bid = new BidModel(req.body);
bid.save(error => {
if (error) {
res.status(500).json({ message: "Could not post bid." });
}
});
const aucById = await AuctionModel.findOneAndUpdate(
{ _id: auctionKey },
{ $push: { bids: bid } }
).exec((error: any, auction: IAuction) => {
if (error) {
res.status(500).json({ message: "Could not post bid." });
} else {
res.status(201).json({ bid });
}
});
};
出于任何原因,如果这两个(save
出价和 findOneAndUpdate
)中的任何一个抛出任何错误,我不希望将任何内容保存到数据库中。我的意思是说要么他们应该保存和更新,要么什么都不应该对数据库进行。
我曾尝试使用 mongoose 会话和事务,但出现此错误。
MongoError: This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.
在这种情况下有什么方法可以解决吗?
如果我理解你的问题是对的,你可以直接删除在以下位置创建的文档:
.exec((error: any, auction: IAuction) => {
if (error) {
// here, by using .deleteOne()
res.status(500).json({ message: "Could not post bid." });
}
或者只是改变你的代码结构,所以只有当两个成功创建时,它们才会被保存并发送响应。
这是我的出价模型。
const BidSchema = new Schema({
auctionKey: {
type: mongoose.Types.ObjectId,
ref: "Auction",
required: true
},
amount: { type: String, required: true },
userName: { type: String, required: true },
});
还有,这是我的拍卖模型(注意这两个模型之间的关系)。
const AuctionSchema = new Schema({
title: { type: String, required: true },
startDate: { type: Date, required: true },
closeDate: { type: Date, required: true },
initialBidAmount: { type: Number, required: true },
bidIncrementAmount: { type: Number, required: true },
bids: [
{
type: mongoose.Types.ObjectId,
ref: 'Bid'
}
]
});
当用户为任何拍卖出价时,我将出价保存在出价收集中并使用 mongoose 更新拍卖收集 findOneAndUpdate
。
const postBid = async (req, res, next) => {
const { auctionKey } = req.body;
const bid = new BidModel(req.body);
bid.save(error => {
if (error) {
res.status(500).json({ message: "Could not post bid." });
}
});
const aucById = await AuctionModel.findOneAndUpdate(
{ _id: auctionKey },
{ $push: { bids: bid } }
).exec((error: any, auction: IAuction) => {
if (error) {
res.status(500).json({ message: "Could not post bid." });
} else {
res.status(201).json({ bid });
}
});
};
出于任何原因,如果这两个(save
出价和 findOneAndUpdate
)中的任何一个抛出任何错误,我不希望将任何内容保存到数据库中。我的意思是说要么他们应该保存和更新,要么什么都不应该对数据库进行。
我曾尝试使用 mongoose 会话和事务,但出现此错误。
MongoError: This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.
在这种情况下有什么方法可以解决吗?
如果我理解你的问题是对的,你可以直接删除在以下位置创建的文档:
.exec((error: any, auction: IAuction) => {
if (error) {
// here, by using .deleteOne()
res.status(500).json({ message: "Could not post bid." });
}
或者只是改变你的代码结构,所以只有当两个成功创建时,它们才会被保存并发送响应。