如下所示,我有一个查询,检查文件名是否已经存在,只是添加一个时间戳使其唯一,现在时间戳太长了,我想使用序数后缀而不是时间戳。每当找到现有文件时,我们如何添加原始后缀?
类似hellofilename-1st,hellofileaname-2nd的东西。等等
Btw文件名是数据库中的列。
#CODE
ngOnInit()
{
this.subscription=this._router.events
.filter((e => e instanceof ActivationEnd)).subscribe((e) =>
{
if(this._router.url.indexOf('login')<0){
this.sessionTimeout(this._router,this.idle,this.keepalive,this.ngbModal);
}
});
}
sessionTimeout( _router:Router,idle: Idle, keepalive: Keepalive,ngbModal: NgbModal) {
// method here
}
#code 2
// check if filename already exists
const file = await context.service.Model.findOne({
where: { humanId: record.id, filename: data.filename },
paranoid: false,
});
if (file) {
const prefix = Date.now().toString();
// eslint-disable-next-line no-undef
const fileParts = data.filename.split('.');
filename = `${fileParts[0]}-${prefix}.${fileParts[1]}`;
}
答案 0 :(得分:0)
您需要尝试使用regexp对所有文档进行计数,然后如果count!== 0,则添加名称为
的文档`hellofilename-${ordinal_suffix_of(count)}`
我不确定您使用的是哪个数据库,如果是MongoDB,则可以在查询中使用$ regex:https://docs.mongodb.com/manual/reference/operator/query/regex/
和方法计数:https://docs.mongodb.com/manual/reference/method/db.collection.count/