What is the best way to retrieve a TTL or expired document in MongoDB?

Is it possible to create a ticketing system where tickets are automatically cancelled after a set period of time? I plan on using MongoDB's indexing feature for deletion, but I also want to save expired tickets in a separate collection for future reference. Can MongoDB facilitate this functionality?

Answer №1

As of the current MongoDB version, which is 5.0.8 at the time of writing this, direct support for your specific task is not available. However, it is expected to be included in MongoDB 6.0 (refer to this jira ticket). In the meantime, there is a workaround that you can utilize.

To elaborate, what you are aiming to achieve involves:

  • Configuring a TTL index to automatically remove documents from your MongoDB collection after a specified amount of time.
  • Establishing Change Streams on this collection with a filter set to capture delete operations exclusively.

In version 5.0.8, the change stream event will only include the _id field of the deleted document as that is the sole information currently present in the oplog.

With version 6.0, you will gain access to the previous state of the deleted document.

Nevertheless, there exists a workaround detailed by Pavel Duchovny in his blog post. By implementing his notification system, you can effectively achieve your intended functionality.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for managing various errors in an Express API - such as addressing 404, 405, 400, and 500 errors

I am still learning the ropes of node.js and am using the express framework to create a REST API. I am looking to manage multiple errors for my API, specifically handling 404, 405, 400, and 500 errors. While express provides a default error handler, I am u ...

Creating a dynamic form field using JavaScript

I'm struggling with a JavaScript issue that requires some assistance. I have a form sending an exact number of inputs to be filled to a PHP file, and now I want to create a preview using jQuery or JavaScript. The challenge lies in dynamically capturin ...

Having trouble with React useref in typescript - encountering an error

Currently, I am in the process of developing a tabs component using React and TypeScript. This component allows users to scroll through tab links from left to right at the top, with overflow: scroll enabled. While this functionality works seamlessly with a ...

Seeking clarity on which specific section of this Node.js code is overseeing the routing of my POST request to the mongoose/mongodb server

Hey everyone, I'm diving into the world of programming and currently enrolled in a Udemy course on web development. As I progress through the lessons, there's one aspect of the code that's leaving me confused. I've got my MongoDB databa ...

Making cross-origin ajax requests without the use of jQuery

I'm encountering an issue with my current code snippet: function ajax(callback, requestString){ console.log("basic ajax sending"); var xmlhttp; // compatible with IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); ...

Unable to locate 'this' in the callback function triggered by tinymce

I am experiencing an issue with my Angular2 app that uses tinyMce as an HTML editor. The problem arises when I need to convert the URLs in the HTML content using my REST API. I tried using the "urlconverter_callback" function from tinyMce, but I encountere ...

Error: The function $(...).multiselect is not recognized

I'm encountering an issue that I can't seem to figure out, receiving the following error message: Uncaught TypeError: $(...).multiselect is not a function at HTMLDocument. (Register:715) at fire (jquery-1.10.2.js:3062) at Object.fireWith [as r ...

Utilizing AJAX and JavaScript to generate a table using the AJAX response and placing it within a <div> element

I am currently passing the response of this action in text form, but I would like to display it in a table format. Is there a way to do this? function loadAditivos(){ $('#aditivoAbertoInformacoesTexto').html('<div id="loaderMaior ...

Saving a single ID at a time in a UseState array by clicking in ReactJS

When clicking on the "click here" button, I am trying to save favorite post IDs in an array. However, the issue is that it is currently only saving one ID at a time in the array. Every time you click on another "click here" button, it removes the previous ...

Generating a fresh array based on the size of its existing elements is the key feature of the ForEach method

When running this forEach loop in the console, it extracts the property "monto_gasto" from an array of objects in the firebase database. Here's how it looks: something.subscribe(res => { this.ingresos = res; ...

Tips on incorporating two MongoDB collections into my Express router?

router.get('/home', ensureAuthenticated, (req, res) => res.render('home', {user_mv: req.user, post_mv: req.post1})); //Create topic page router.get('/create-topic', ensureAuthenticated, (req, res) => res.render('cr ...

Saving a PHP form with multiple entries automatically and storing it in a mysqli database using Ajax

My form includes multiple tabs, each containing various items such as textboxes, radio buttons, and drop-down boxes. I need to save the content either after 15 seconds of idle time or when the user clicks on the submit button. All tab content will be saved ...

How can I delete a character in a json object at a certain index using javascript?

Here is a JSON code snippet: [ [ { "title":"Shirt Front", "thumbnail":"http://ipadd/pub/media/catalog/product//b/l/blue-back_2.jpg", "elements":[ { "type":"image", "so ...

Gather data from all input sources and compile it into a well-organized

I'm looking to convert HTML form inputs into JSON format. Initially, I attempted to achieve this using jQuery's serializeArray method, which worked fine for simple input names. However, when dealing with multidimensional input names like: <i ...

Adjust Sidebar Height to Match Document Height (using React Pro Sidebar)

Having an issue with the height of a sidebar component in Next.js using React Pro Sidebar. It seems to be a JavaScript, HTML, and CSS related problem. I've tried several suggested solutions from Stack Overflow, but none of them seem to work. Surprisin ...

What is the best way to automate sending emails for every error that arises in Node.js?

In the scenario where my node.js application is up and running, I am looking for a way to handle all types of errors (not just web errors) and have a function that can send an email notification when an error occurs. Essentially, before the error gets wr ...

Creating a sequence of HTTP calls that call upon themselves using RxJs operators

When retrieving data by passing pageIndex (1) and pageSize (500) for each HTTP call, I use the following method: this.demoService.geList(1, 500).subscribe(data => { this.data = data.items; }); If the response contains a property called isMore with ...

Encountering an UnAuthorized Error when attempting to decode a JWT token on an app route using express-jwt with RS256 encryption

Currently, I am using Keycloak to handle authentication and have successfully attached a token to my request using its middleware. However, when attempting to access user data through an API route that utilizes this token, I encounter the following error: ...

Automatically showcase images from a directory upon webpage loading

Is there a way to modify my code so that the images from the first directory are displayed on the page when it loads, instead of waiting for a menu option to be clicked? The page looks empty until a menu option is selected, and I would like the images to s ...

Unable to transform SVG files in Next.js Jest version 28

Currently, my setup involves Next.js version 12.0.7, Jest version 28.1.0, and the use of the next-react-svg library for importing SVG files into components. The configuration in my jest.config.js file looks like this: // jest.config.js const nextJest = re ...