Search code examples
databasemongodbpostgresqltypeormtypeorm-activerecord

Searching data older than a Date with typeORM


I am executing a query to Postgre DB to fetch data older than a specific date.

Here's my function

async filesListToDelete(): Promise<any> {
  return await this.fileRepository.find({
    where: { last_modified: { $lt: '2018-11-15 10:41:30.746877' } },
  });
}

Here's how I defined my File entity:

export class File {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ nullable: false })
  idFonc: number;

  @Column({ nullable: false })
  version: number;

  @Column('varchar', { length: 100, nullable: false })
  filename: string;

  @Column({ nullable: true })
  last_modified: Date;

  @Column({ nullable: false })
  device: boolean;

  @ManyToOne(type => Type, { nullable: false })
  @JoinColumn({ referencedColumnName: 'id' })
  type: Type;

  @OneToMany(type => FileDevice, filedevice => filedevice.file)
  fileDevice: FileDevice[];
}

I get this error

QueryFailedError: invalid input syntax for type timestamp: "{"$lt":"2018-11-15 10:41:30.746877"}"

Solution

  • You can use LessThan, the doc

    async filesListToDelete(): Promise<any> {
      return await this.fileRepository.find({
       where: { 
           last_modified:  LessThan('2018-11-15  10:41:30.746877') },
    });}