Update one Database-Collection based und values in another collection

Hi everybody,
I have just started using Appsmith and JS (coming from C-based programming, so I´m quite new to JS).
After struggling a bit with JS, I have managed to set up an App that reads Data from a MongoDB and writes changes to it. The app should be used to administrate workplaces and resources for our students. I have one collection for the students containing Names, information and the assigned workplaces and hardware and one for the workspaces (another for the hardware shall follow).
Now I have to update the occupied-flags of the Workplaces-collection (boolean) based on the selection in the Students-Collection.
I have a query for the selection-dropown that collects only unoccupied-flagged workplaces but I am completly lost how to update the flags by means of the used workplaces in the Students table.
Is there anyone with an idea how to solve this problem?
Normally I would generate an true/false array and use this to iterativly update the values in the workplaces collection but I can not tell how to do this in this enviroment or wether this is feasible.
I am thankful for any help.
Best regards

Marius

Ok, I have made some headway. I can get the IDs of the Collection I want to edit. But I can´t manage to update just one parameter of the Object in the MongoDB. Can anyone tell me the syntax how to do this?
I have a JS-Object that returns a list of IDs. At these IDs I want to chance the parameter “Occupied” to true or false. But I can´t get the Update Query to do so.
Thanks for all help.
Best regards.

Marius

Hello Marius and welcome to our community! Could you please share some screenshots of your code snippets and the error you get?

Hi Amelia,
thanks for the welcome.
I have written a JS-function that searches for objects where a the “Occupied-Parameter” is set to “true” (boolean).
This function called “Get_Used_WP_IDs” returns a list of object IDs
[
“637ccc886a7b584f66857484”,
“637ccc886a7b584f6685749a”,
“637ccc886a7b584f66857486”
]

I want to use this list to set the Parameter “Occupied” to true for these Object-IDs.
But I can´t figure out the syntax for the Update-Query, because till now I haven´t fully understood it.
As I said I´m a beginner. Maybe the solution is quite easy.
I hope this helps to understand my problem.
Best regards

Marius

For testing I´m currently using a fixed ID (copy/paste from MongoDB) and
Collection: Arbeitsplätze

for Query:
{ _id: “637ccc886a7b584f66857484” }
Edit: Correct is :
{ _id: ObjectId(‘637ccc886a7b584f66857484’) }
Edit-end

For Update:

{ “$set”: { “Occupied”: true } }

The Output is:

{
“nModified”: 0
}

Edit: I sorted out the syntax for fixed IDs. Now I need to learn how to use Multiple IDs from the function output. Do I have to call the query one by one with one ID from JS or is it possible to directly update a list of IDs?

Best regards

Marius

Hi Marius! You can update multiple records by using ‘All Matching Documents’ as Limit. In your query, you can use the $in operator to update a list of IDs. Here is an example query:

Hi Amelia,

thank you for your help. I´m now able to manage these things :).

Yesterday we came up with the idea of visualizing time-spans where hardware resources are blocked oder reserved. I found out that appsmith is able to integrade google calender or calendly but we don´t want our calenders to be hosted externally. Is there a possibility to integrate a caldav/ical calender (as we are already hosting one on one of our servers). Or another way to show and analyze available resources?
Thank you for your help.
Best regards

Marius

Hi Marius,

Sure, you should be able to connect to iCal or Caldav, as they provide APIs. Basically, in Appsmith, you can connect to any REST API.

Hi Amelia,

thanks for your support. I have made some good headway but ran into a problem with an JSON-Form field.
I want to populate a dropdown-select with numbers that are stored as strings in the MongoDB.
I´m using a function (Check_Arbeitsplaete) that outputs the following:

[
{
“label”: “1”,
“value”: 1
},
{
“label”: “2”,
“value”: 2
},
{
“label”: “3”,
“value”: 3
},

When I set the Select field to {{Check_Arbeitsplaetze.Arbeitsplatz_Check()}} it works for a moment, at some random point when I come back or select another line from the table it changes to a Number Input or Text Input and I figure out why. These happens very randomly. In the MongoDB the Value is stored as a String and used for a Table and the JSON-Form to edit this data. I tried populating the Select with Strings or Numbers but there is no difference.
Best regards

Marius

Hi Marius! Could you please send us a loom.com screen recording with your implementation and the issue to help us better understand it?

Hi Amelia,
I will do so tomorrow. Meanwhile I ran into another problem.
I have to suspend a running function, open a modal for a user decision, but the only way to do that was to set the code into a while loop which seems not very smart and I don´t manage to break the loop with an externall Input. Is there a way to suspend running code and wait for a user decision?
Thanks again for your great support.
Best regards

Marius

Hi Marius! I think the best way would be to use 2 JS functions: the first one would open the modal before finishing to run, and the second one would do the rest of operations and would be triggered once the user submits the input (e.g., on click of the Confirm button).

Hi Amelia,

sorry for “Multithreading”.
After a busy time at the end of the year I solved the problem with the modal, but still have the issue with the Select Input which keeps changing. I´ve recorded the problem. Here is the loom link:

I have tried to parse the Value Output of the function to Int but it didn´t change the behaviour.
Thanks again for your help.
Best

Marius

Hi Marius! I am not able to reproduce this issue. Can you please share this app with us to debug this scenario? Add support@appsmith.com as a Developer and send us the link to the app.

Hi Amelia,
thanks for your help, I will do so when I have fixed another Problem.
I have written a function that returns a boolean Value depending wether a field in a certain ID ist empty or not. The function is as follows:

export default {

Reservation_Check: async(ID) => {
	let Reservation = true
	await Get_Arbeitsplaetze.run()
	const WP_Data = Get_Arbeitsplaetze.data

for(let i = 0; i < WP_Data.length; i++)
{
if(WP_Data[i]._id === ID)
{
if(WP_Data[i].RFrom.length < 2 && WP_Data[i].RTill.length < 2)
{Reservation = false}
else
{Reservation = true}
}
}
return Reservation
}
}

This basically searches for the Input-ID and checks if the two fields are empty. “Reservation” is than either true or false.

If I want to execute Code based on this if-structure

if(Check_WP_Reservation.Reservation_Check(JSON_WP_ID) === false)

The if loop is not entered anymore. Yesterday I had other similar calls working fine. Now they do not work anymore and a new one is skipped as well.
If I check the type of return value from the function it is an object and not a boolean.
Do you have any idea whats wrong here? I´m 100% shure that the other if loops yesterday were executed as I tested this part of the code very thoroughly.
Thanks again for your help :).
Best

Marius

Can you share a loom.com screen recording showing this issue in action?

Hi Amelia,

I´ve tried to show what happens by a test function call. It seems like I make a mistake with the call but the part of code I´m showing at the end has the same if statement, but I´m shure it was executed as I developed this part beforehand and had to test it many times.

Thank you for your help.
Best regards

Marius

Solved it, or at least got it working again.
It seems that the “async” seems to cause a problem, I inserted an await-call to pull database data. This causes the problem.
If I´m carefull it is ok, but ist there some reason for this behaviour?
Best

Marius

Could you please elaborate on the issue you found and how you solved it to help us better understand this?

Hi Amelia,

this problem seems to come from my lack of understanding JS async/promise etc. I got it working but don´t seem to have completly grasp the root of the problem. At the moment I got my calls working by being careful and using a bit of “try and error” solving the problems. I will come back later to this problem, the problem with the select-dropdown is still to be solved, but at some late point.

My current problems are more performance issues. To keep my database synced I have to make many querys and for loops to access fields in MongoDB Objects. At the Moment my only possibility to access single datafield in an object in the DB is to query the hole collection and then ran through it with a for loop, to check where the id matches my and then extract the Data from the array.
Is there a way to directly access on field in a MongoDB object, when I know the ObjectId? I have query that should to the trick, but it returns the Object ID as well, so can not really use it.

When I want to Get the “Arbeitsplatz-Nummer” of a student (“StudentName” is just a placeholder)
{
“find”: Students,
“filter”: { Name: StudentName },
“projection”: { Arbeitsplatz: 1 },
“sort”: { },
“limit”: 1
}

It returns:
[
{
“_id”: “6389dda29be33482ef4f5e7c”,
“Arbeitsplatz”: “42”
}
]

Is there a way like “ObjectID”.Arbeitsplatz or something like that?
All the other processes or quite time consuming as the are many and so a change in the DB (with checks for time-collisions etc) takes a good minute at the moment.
Thanks again for yout help. It is most appreciated
Best

Marius