I am using \"mongoose\": \"^5.7.1\"
in my Node.js project. I am making an api which involves updating in two documents. So, I am using the transactions like fol
As suggested in accepted answer, you need to have your local server to be run as a replica set to be able to perform transactions, as opposed to standalone server.
However, in addition to the proposed solution, you can easily convert your Standalone local db to a Replica Set without using any third-party tool, by following instructions in MongoDB documentation, summarized as follows:
replSet
argument.mongod --port 27017 --dbpath /srv/mongodb/db0 --replSet rs0 --bind_ip localhost
mongo
shell, and initiate the new Replica Set.rs.initiate()
Now you should have a Replica Set instead of a Standalone mongodb server, where you can perform transactions on your local environment to update multiple documents at once!
Do not forget to include the replSet
argument every time you want to start the server, otherwise it will be started as Standalone. I simply use the same command as in step 1 to run it again.
Alternatively, you can deploy a new Replica Set from scratch for testing environment following these other instructions in MongoDB documentation.
Please Edit App/Config/database
file Add 'retryWrites'=>false
in Mongodb Connection String
write Mongo db connection
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', 'lo*****'),
'port' => *****,
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => [
'database'=> env('MONGO_DB_DATABASE'),
'retryWrites'=>false
]
],
Save And Run
Transactions are undoubtedly the most exciting new feature in MongoDB 4.0. But unfortunately, most tools for installing and running MongoDB start a standalone server as opposed to a replica set. If you try to start a session on a standalone server, you'll get this error.
This issue can be resolved by using replica-sets on your local environment.
I have used run-rs for this purpose.
&retryWrites=false
to your connection string--
I actually got the errors mentioned in the OP when connecting to our remote db server, whereas it was working locally. I contacted our mongo hosted support before trying the suggestion that is in the error.
--
This is what our hosted mongo site (mLab) said :
It's likely your app's driver was updated to a more recent version which is attempting to use a WiredTiger-only feature. As the error mentions, you'll need to add &retryWrites=false to your connection string.
https://docs.mlab.com/faq/#why-am-i-getting-the-transaction-numbers-are-only-allowed-on-storage-engines-that-support-document-level-locking-error