
I have defined an attribute that is an array of strings. Now, I would like to know how to add a new string element to the array. What would be the appropriate query or method to accomplish this?


How to update an array attribute?

The example for that it would be something like this, will assume the field phones is an array and you'll like to add one phone.
const newPhone = '131331';
try {
// Get the current document
const doc = await database.getDocument('[DATABASE_ID]', '[COLLECTION_ID]', '[DOCUMENT_ID]');
// Updating the document. adding the phone at the end of the array attribute
await const promise = databases.updateDocument('[DATABASE_ID]', '[COLLECTION_ID]', '[DOCUMENT_ID]',{
phones: [...doc.phones, newPhone]
});
} catch (e) {
// handle error.
}

but everytime a unique documentid goes in the DB so how do we get that ??

i approached it something like this const promise0 = await databases.listDocuments('DATABASEID', 'COLLECTIONID', [ Query.equal('Email', Email) ]);
if (promise0.total === 1) { let arr = promise0.documents[0].writtenthings; arr.push(content); const promise = databases.updateDocument( 'DATABASEID', 'COLLECTIONID', promise0.documents[0].$id, { writtenthings: arr } ); }
if (promise0.total === 0) { const arr = [content]; const promise = databases.createDocument( 'DATABASEID', 'COLLECTIONID', 'unique()', { Email: Email, writtenthings: arr } ); }

[CLOSED] How to update an array attribute?
Recommended threads
- [Node.js SDK] Bypass 2GB file limit?
Hello. Using either InputFile.fromPath or InputFile.fromBuffer throws this error: File size (2295467305) is greater than 2 GiB Bucket limit etc. is setup corre...
- Relationship null, even when relationshi...
Hi Everyone, im experiencing issues with set relation data. When im setting the document id from the related database most of them seem fine, except one table. ...
- Website shows just the HTML in safari
Hi y'all, I've been working on a project for a while but, for some reason it just shows the html in safari, It work perfectly fine in firefox and chrome, but sa...
