MongoDB Transactions in cloud code

From what I read it is still not possible to use mongoDB transactions in cloud code using Parse. However I really need this feature, is it possible to get the mongoDB instance from parse or do I need to make another connection to do this?

I think it is supported, just not yet by the JS SDK:

  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-Parse-Application-Id': 'test',
  url: 'http://localhost:8378/1/batch',
  body: JSON.stringify({
    requests: [
        method: 'PUT',
        path: '/1/classes/MyObject/' +,
        body: { myAttribute: 'myValue' },
    transaction: true,

Otherwise you can access the mongoDB adapter directly via Parse.Server.database.adapter

You have to use the MongoDB driver.

Here is the code I use.

In a utility file :

function getDatabaseController() {
const config = Config.get(Parse.applicationId);
return config.database;

async function getMongoCollection(className) {
const mongoAdapter = getDatabaseController().adapter;
await mongoAdapter.connect();
return mongoAdapter.database.collection(className);


  • Get mongodb session - used for transaction

  • @returns {Promise<*>}
    async function getMongoSession() {
    const mongoAdapter = getDatabaseController().adapter;
    await mongoAdapter.connect();

    return mongoAdapter.client.startSession();

Then I do my own withTransaction function with :

	await session.withTransaction(async () => {
		//Here we return the value from callback
		result = await fn(session);
	}, {
		readConcern: { level: 'snapshot' },
		writeConcern: { w: 'majority' }

surrounded by try, catch, finaly (with logs)

Hope that helps

1 Like

This seems a bit complex, we also need the similar feature, will Parse Sever wrap mongodb transaction feature at its layer?

Thanks v much.

Best regards, Jason

@xeoshow @Kone, I can confirm that @oallouch is correct.

In general, using transactions is linked to specific business logic involving complex operations. The safest approach is to utilize the client (Mongo or Postgres) and the tools provided by the database provider.

It may appear complex at first, but once you understand the process, it becomes straightforward. Also, don’t forget that in the case of Mongo, you need to pass the ‘session’ object in each operation within the ‘withTransaction’ function; otherwise, these operations will not be transactional.

A pattern I use is, when I need complex querying :

  1. I use an aggregation or a normal query using the driver
  2. that query returns only ids
  3. I use those ids in a Parse query with all its comfort (include, …)