![](https://cdn.discordapp.com/avatars/1088470403539800175/37741721fd5565030eca3f7dee7f2ff5.webp)
Hi, this a function in dart that subtract a 1 from the LMMS attribute for 6000 documents then update every document with the new subtracted value, the issue is, it takes so long (11 minutes)😭
var response = await database.listDocuments( databaseId: databaseId, collectionId: collectionId, queries: [Query.limit(6000)]); var documents = response.documents;
try { for (var doc = 0; doc < documents.length; doc++) { String documentId = documents[doc].$id; final data = documents[doc].data;
// Subtract 1 from LMMS first
int LMMS = data['LMMS'] - 1;
// Update the document with the initially subtracted LMMS
await database.updateDocument(
databaseId: databaseId,
collectionId: collectionId,
documentId: documentId,
data: {'LMMS': LMMS});
}
return context.res.send(200);
} catch (e) { print('Error updating document: $e'); return context.res.send(401); }
Could any one help me make it faster.
![](https://cdn.discordapp.com/avatars/186656408450629633/ca3bdd514fde110c200d8e9cff0c1328.webp)
This is a function I've used in the past to update a lot of existing documents, you might see if it helps out any.
import { Client, Databases, Query } from 'node-appwrite';
export default async ({ req, res, log, error }) => {
const client = new Client()
.setEndpoint(process.env.APPWRITE_FUNCTION_ENDPOINT)
.setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
.setKey(process.env.APPWRITE_FUNCTION_API_KEY);
const database = new Databases(client);
let response;
const queries = [Query.limit(25), Query.select("$id")];
let cursor = null;
do {
const newQueries = [...queries];
if (cursor) {
newQueries.push(Query.cursorAfter(cursor));
}
response = await database.listDocuments(
DATABASE_ID,
COLLECTION_ID,
newQueries
);
cursor = response.documents[response.documents.length - 1].$id;
await Promise.all(
response.documents.map((document) => {
database.updateDocument(
DATABASE_ID,
COLLECTION_ID,
DOCUMENT_ID,
data
)
})
);
} while (response.documents.length >= 25);
return res.send('Complete!');
};
![](https://cdn.discordapp.com/avatars/1088470403539800175/37741721fd5565030eca3f7dee7f2ff5.webp)
![](https://cdn.discordapp.com/avatars/186656408450629633/ca3bdd514fde110c200d8e9cff0c1328.webp)
How often are you needing to run this function? Is it a one and done or ?
![](https://cdn.discordapp.com/avatars/1088470403539800175/37741721fd5565030eca3f7dee7f2ff5.webp)
It will run a lot
![](https://cdn.discordapp.com/avatars/1088470403539800175/37741721fd5565030eca3f7dee7f2ff5.webp)
That's why I'm frustrated it takes 11 minutes to complete for only 6000 document
Recommended threads
- redirect_uri_mismatch in Google OAuth2
Hi all. I'm hosting my appwrite on my own VPS but I', having trouble creating a session for an authenticated user via gmail. When I run the appwrite endpoint wi...
- Private identifiers are only available w...
Hello folks, hope you all doing fine. Have any of you faced the error below? ```sh node_modules/node-appwrite/dist/id.d.ts:2:5 - error TS18028: Private iden...
- Graceful Error Handling with account.get...
Dear community, as I understood the best way to check for an active session is account.get() and if it returns an 401 there is no active session. Now I would pr...
![](/images/bgs/pre-footer.png)