Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Error: Cannot use retryable writes with limit=0

I'm currently working on my first node.js rest api with express, mongodb (atlas cloud) and mongoose, when i try to make a .remove request i get this error:

{
"error": {
    "name": "MongoError",
    "message": "Cannot use (or request) retryable writes with limit=0",
    "driver": true,
    "index": 0,
    "code": 72,
    "errmsg": "Cannot use (or request) retryable writes with limit=0"
}

This is my request:

router.delete('/:productId', (req, res, next) => {
const id = req.params.productId;
Product.remove({ _id: id })
    .exec()
    .then(result => {
        res.status(200).json(result);
    })
    .catch(err => {
        console.log(err);
        res.status(500).json({
            error: err
        })
    }); ;
});
like image 264
gonzarodriguezt Avatar asked May 10 '18 23:05

gonzarodriguezt


People also ask

What is retryable writes in MongoDB?

MongoDB retryable writes make only one retry attempt. This helps address transient network errors and replica set elections, but not persistent network errors.

Does MongoDB support retryable writes and reads?

Official MongoDB drivers compatible with MongoDB Server 4.2 and later support retryable reads. For more information on official MongoDB drivers, see MongoDB Drivers. Drivers can only retry read operations if connected to MongoDB Server 3.6 or later.

What is MongoDB Mongosh?

The MongoDB Shell, mongosh , is a fully functional JavaScript and Node.js 16.x REPL environment for interacting with MongoDB deployments. You can use the MongoDB Shell to test queries and operations directly with your database. mongosh is available as a standalone package in the MongoDB Download Center.


2 Answers

The findOneAndRemove() function would work more accordingly since its specific to the filtering method passed in the function .findOneAndRemove(filter, options) to remove the filtered object. Still, if the remove process is interrupted by the connection the retryRewrites=true will attempt the execution of the function when connected.

More information here

When using retryRewrites set to true tells the MongoDB to retry the same process again which in fact can help prevent failed connections to the database and operate correctly, so having it turn on is recommended.

More info here

If you are using Mongoose 5^ and MongoDB 3.6 your code is better written like:

mongoose.connect('mongodb.....mongodb.net/test?retryWrites=true', (err) => {
if(err){
    console.log("Could not connect to MongoDB (DATA CENTER) ");
    }else{
        console.log("DATA CENTER - Connected")
    }
});// CONNECTING TO MONGODB v. 3.6

router.delete('/:productId', (req, res, next) => {
const id = req.params.productId;
Product.findOneAndRemove({ _id: id })//updated function from .remove()
    .exec()
    .then(result => {
        res.status(200).json({
       message: "Product Removed Successfuly"
     });
    })
    .catch(err => {
        console.log(err);
        res.status(500).json({
            error: err
        })
    }); ;
});
like image 154
Ricardo Alves Avatar answered Oct 21 '22 05:10

Ricardo Alves


I just changed the true to false in retryWrites=true and it worked. Is that a good approach? Or there is a better way to solve this problem?

like image 43
thegreathypocrite Avatar answered Oct 21 '22 03:10

thegreathypocrite