I’m trying to write a trigger script – I think a very simple one.
First Q: Do manual updates in MongoDB trigger triggers?
Main Q: How do I do this? …
I have a posts_md collection. It has an insert/update trigger. I want to copy every document to another posts_html collection. This is my attempt at code:
Here’s I’m changing one field: contentType from md to html. The ultimate goal is to convert the markdown to html as wellof course, regular CMS stuff. But first I’m just trying to get the basic document copying over.
Which is closer to has worked for me in the past using the mongodb frontend library. I tried that other way based in the extremely limited examples in the trigger documentation ( https://docs.mongodb.com/realm/triggers/trigger-snippets/ ).
Neither use of the updateOne() arguments has given me any results.
Is there a clear documentation on how trigger scripts are supposed to run?
I really feel like what I’m writing here should be EXACTLY what dozens of other CMS do, isn’t this a standard use case?
Yes, I definitely do want to upsert. Initially the html entry won’t exist, the first time it needs to be created. In the future it matches by slug. (which I get will break if the slug breaks, I should update a reference to an id, but I’m still just trying to get the basics working.)
Still does not work though. How exactly should this update statement work?
This code looks like it has some issues , for example chnageEvent.collection is not a reference to the collection itself and i don’t believe has an updateOne function.
Additionally there are const declared values which you are changing. So there are fundamental js issues here, does this code run without errors??
Please share the trigger web url so I can have a look.
Look at my blog series here it contains some similar trigger manipulation maybe it will provide you with the way to write this one. It has an async and await trigger syntax as it is much cleaner and advised.
This code looks like it has some issues , for example
chnageEvent.collection is not a reference to the collection
itself and i don’t believe has an updateOne function.
I can just grab a reference to the collection from scratch I guess. Surprising the changeEvent doesn’t have one?
Additionally there are const declared values which you are
changing. So there are fundamental js issues here, does this
code run without errors??
In javascript I should be able to change the properties of a const object – and I expect that to be the correct use here? Something seems to definitely be wrong with the fullDocument though.
Please share the trigger web url so I can have a look.
I don’t know the web url? This is a trigger, not a web hook.
Look at my blog series here it contains some similar trigger manipulation
maybe it will provide you with the way to write this one.
It has an async and await trigger syntax as it is much cleaner and advised.
Thanks, ok… yeah await and async work as expected. The trigger web editor just doesn’t like it.
With the help if your examples, I can add a brand new entry to another collection. So it’s definitely the fullDocument that’s failing.
WHERE CAN I get some reference and help how to work with changeEvent and fullDocument?
I copy over the fullDocument every time… I know I know only the changed fields. But the point of this is to only run when a blog post is update, rare, so I’d rather make sure everything copied will than care about that level of efficiency?
Even if you use atlas trigger you still have a realm app in the realm tab.
What I meant by url of the trigger is basically copy the url in the browser when you are editing the trigger UI/function. This way I cN view code and config…