I set expiry time to 24 hours, but the documents expire after around 5-10 minutes (I haven't timed it exactly). What am I doing wrong? My schema:

const collectionSchema = new mongoose.Schema({
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User"
  },
  name: {
    type: String,
    maxLength: 30,
    required: true
  },
  entries: [{ type: mongoose.Schema.Types.ObjectId, ref: "Entry" }],
  expireAt: { type: Date, expires: 60 * 60 * 24 }
});

In the post route, I conditionally set the date so that inlogged clients get data persistence.

router.post("/", auth, async (req, res) => {
  let date = null;
  if (!req.user) {
    date = new Date();
  }
  try {
    const collection = {
      userId: req.body.userId,
      name: req.body.name,
      expireAt: date
    };
    const newCollection = await Collection.create(collection);
    res.send(newCollection);
  } catch (error) {
    res.send(error.message);
  }
});

I thought I had a time-zone problem, but when I check the time stamp in MongoDB compass, it matches my time-zone. What am I doing wrong?

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.