You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB无法删除数据+启用retryWrites仍报limit=0错误求助

Fixing "Cannot use (or request) retryable writes with limit=0" Error & MongoDB Delete Issues in Node.js/Express

Hey there, let's tackle this error and your delete problem step by step. The Cannot use (or request) retryable writes with limit=0 error usually stems from mismatched MongoDB connection settings or deprecated Mongoose methods. Here's how to resolve both issues:

1. Update Your MongoDB Connection String

You already added retryWrites=true, but retryable writes require a write concern of majority to work properly. Your current string is missing this parameter.

Modified Connection String:

mongoose.connect('mongodb+srv://admin:admin@rest-shop-e3hfo.mongodb.net/test?retryWrites=true&w=majority')
  .then(() => console.log("Successfully connected to MongoDB!"))
  .catch(err => console.error("MongoDB connection failed:", err));
  • Added &w=majority to enforce the required write concern
  • Added connection success/error logging to verify if your DB connection works as expected

2. Replace Deprecated remove() Method

Mongoose's remove() method is deprecated and can cause unexpected behavior (like your delete failure). Use deleteOne() instead, which is the modern, supported method for single-document deletion.

Modified Delete Route in Orders Module:

router.delete('/:orderId', (req, res, next) => {
  Order.deleteOne({_id: req.params.orderId})
    .then(result => {
      if (result.deletedCount === 0) {
        return res.status(404).json({ message: "Order not found" });
      }
      res.status(200).json({ message: 'Order deleted' });
    })
    .catch(err => {
      console.log(err);
      res.status(500).json({ error: err });
    });
});
  • Swapped Order.remove() with Order.deleteOne()
  • Added a check for deletedCount to return a 404 if no order was found (improves error handling)

3. Verify Mongoose Version

Outdated Mongoose versions may have compatibility issues with MongoDB's retryable writes feature. Update to the latest stable version to rule this out:

npm install mongoose@latest

4. Quick Check: Ensure Your Database User Has Delete Permissions

Double-check that your MongoDB Atlas user (admin in this case) has the necessary permissions to perform delete operations on the test database. You can verify this in the MongoDB Atlas dashboard under "Database Access".

After making these changes, restart your server and test the delete endpoint again—both the retryable writes error and deletion failure should be resolved.

内容的提问来源于stack exchange,提问作者Oleksandr Tatarinov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:13:16