Overriding oldest item in the collection

Hey,
I’m building an announcement feed that will display important notes on the page.
I’m using this example for dynamic content from a collection:
https://www.wix.com/corvid/example/dynamic-slideshow

However as there is no option to add more slides dynamically, I want to limit the collection size to 6 and on each item added to the collection to remove the oldest item.

What is the best way to approach this problem?

Hi Edward,

You could query on the hidden field called _createdDate in ascending order. That would be a way to isolate the oldest item. Then, issue the remove command.

wixData.query("announcements")
.ascending("_createdDate")
.limit(1)
.find()
.then( (results) => {
    if (results.items.length > 0) {
        console.log(results);
        let item = results.items[0];
        wixData.remove("announcements",item._id)
        .then((result) => {
            console.log(result);
        })
    }
} );

@edwardfrogz You could call it in the Before Insert hook.