Delete Error with Realm SDK


Am I running the command below with the Realm SDK:

const mongo = app.currentUser.mongoClient(“mongodb-atlas”).db(db).collection(collection);
const result = await mongo.deleteMany({});

The problem is, if there are too many documents, I get the following error: The user aborted a request.

How to solve this?

Hi @Bruno_Nobre,

Can you please clarify what too many documents means in this context? The deleteMany({}) command is executed as a function call from the SDK, and as such is subject to the constraints for functions. If you’re on a low tier, you’re also subject to limitations of your throughput: the combinations of these factors dictates the limits you’re likely hitting. Raising the tier would certainly help, you should take into consideration how frequent this type of operation is to size the app properly.


If I have more than 2000 documents, for example, this error already occurs. I use M2 Cluster in the moment.

Why insertMany can insert thousands of documents at once and deleteMany can’t delete 2000?

Why via mongodriver does not have this problem? Mongodriver works much better, but then you can’t work with permissions with users… Or rather you can, but limited to 100 database users. :rage:

Another similar problem.

Do you have Sync enabled on that collection?

I’ve tried to reproduce the issue, and indeed there’s an odd behaviour for the deleteMany(…) function, when called from the SDK: we’ll raise an internal ticket for this.

As a workaround, you can try to implement your own Realm function within your app on the portal, something like, say, wipeCollection defined as

exports = async function(collectionName){
  const mdb ='mongodb-atlas');
  const db = mdb.db('database');
  return await db.collection(collectionName).deleteMany({});

and from your app code, do

const result = await app.currentUser.callFunction('wipeCollection', [collection]);


Thanks, I got it and it worked perfectly, but I had to put the system user.

But the ideal is to work by SDK. Realm SDK is much inferior to MongoDB Driver.

They aren’t meant to overlap, the functionality you’ve been using (MongoDB Data Access) is built for convenience to allow simple interaction with the remote DB, however, from a mobile device, the developer will likely either:

  1. Use the powerful local object DB that Realm provides, with automatic synching with the backend, powered by MongoDB (Mobile-first approach): DB logic is then at home on the device, with the possibility of some further processing on the backend, if needed.
  2. Build an online-only app by leveraging the backend functionality, with Triggers, Functions, Webhooks, … (Backend-first approach): the Functions will have the full access to the MongoDB Driver, implement the core logic, and at the same time deliver a clean and secure range of services to the mobile side, without the risk of allowing full access to the DB from mobile clients.

the Functions will have the full access to the MongoDB Driver

Not really, for example in Functions the ReplaceOne is not implemented.

I guess you’re referring to your other post: however, replaceOne(…) is indeed available within a Realm Function, are you experiencing any error there?

I’ve just tested the following:

exports = async function(collectionName){
  const db ='mongodb-atlas').db("database");
  const collection = db.collection(collectionName);
  try {
    let result = await collection.findOne({<query>});
    result.fieldValue = 123.4;
    return await collection.replaceOne({<query>}, result);
  } catch (error) {
    return null;

and it works as expected.

Sorry man,

I was confused, replaceOne works in Functions, but it has a serious problem regarding the MongoDB driver.

I have the following code in Node.JS using MongoDB Driver:

await mongo.db(db).collection(collection).bulkWrite(docs)

This code works perfectly with thousands of ReplaceOne expressions at once (20.000). But if I try to write a Function that does this and send thousands of ReplaceOne expressions through the Realm client, I get an error and get disconnected due to the large amount of information. Even with just hundreds of expressions.

Functions will have full access to MongoDB driver, but MongoDB driver client performance is better in this case with BulkWrite.

This brings us to my previous question:

The reason is that, when Sync is enabled on a collection, every change needs to be recorded to be transmitted to the mobile clients, that will in turn reflect that change on the local DB (see the Mobile-first approach I indicated in an earlier comment). This process limits the amount of changes that can be recorded in a given time, before the Function hits the timeout.

If, OTOH, you plan to use the Backend-first approach, and handle everything via Functions/Webhooks/…, with no local DB on the devices, then having Sync active makes little sense, and you can get a better performance (within the Service Limitations, obviously) by disabling it.

The sync is not activate… Sorry, I didn’t answer before.

Deletion is another example… If I try via mongodb driver it works normal, but via Realm SDK gives an error with only a few hundred documents.