猫鼬参考文件的TTL过期 [英] TTL expiration of Mongoose referenced document

查看:81
本文介绍了猫鼬参考文件的TTL过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在我的应用程序中的文档集合上设置TTL的要求,但是还需要其他文档集引用这些文档,并且不确定如何运行吗?

I have a requirement for setting a TTL on a collection of Docs in my app but also need those docs to be referenced by another collection and am unsure how this would work?

在文档上设置TTL时,该文档过期后会从所有其他参考文档中删除该文档吗?还是我必须自己编写一个后端进程而不使用mongoDb的TTL索引?

Does setting a TTL on a document remove that document from all other reference docs when it is expired or would I have to write a backend process myself and not use mongoDb's TTL indexes?

我想要的功能的示例架构:

An example schema of the functionality I would want :

var mongoose = require('mongoose')
  , Schema = mongoose.Schema

var personSchema = Schema({
    _id     : Number,
    name    : String,
    age     : Number,
    stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});

var storySchema = Schema({
    _creator : { type: Number, ref: 'Person' },
    createdAt: { type: Date, expires: 60*60*24 },
    title    : String,
    fans     : [{ type: Number, ref: 'Person' }]
});

var Story  = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);

因此,一天之后,故事文档将过期,而我需要该故事的引用才能从该人那里过期

So after a Day the Story doc would expire and i would need the ref to that story to expire from the person

推荐答案

您无法使用TTL索引处理此问题. 您可以使用cron作业/计划任务来删除旧文档.

You cannot handle this with TTL indexes. You could use a cron job/scheduled task to remove old documents.

我建议您阅读,这是在MongoDb上实现后台进程的一种很好的轻量级方法.

I'd suggest you read this, a nice lightweight way of implementing background processes on MongoDb.

这篇关于猫鼬参考文件的TTL过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆