我有 app、 type、ongo。 这些版本是:
"@nestjs/common": "^8.0.0",
"@nestjs/typeorm": "^8.0.4",
"mongodb": "^4.6.0",
"typeorm": "^0.3.6"
我试图以这种方式对蒙戈邦文件进行部分搜查,这是我完整的服务档案。
import { Injectable } from @nestjs/common ;
import { InjectRepository } from @nestjs/typeorm ;
import { ILike, Like, MongoRepository, Raw, Repository } from typeorm ;
import { City } from ./city.entity ;
@Injectable()
export class AppService {
constructor(
@InjectRepository(City)
private citiesRepository: MongoRepository<City>,
) {}
getSuggestions(query: string): Promise<City[]> {
console.log( in fun, query , query);
// return this.citiesRepository.findBy({
// name: Like(`%${query}%`)
// });
return this.citiesRepository.find({
where: {
// "name": { "$regex": ".*a.*"}
// name: Like(`%${query}%`)
// name: Like( a )
// name: new RegExp(`^${query}`)
// name: Raw(alias => `${alias} ILIKE %${query}% `),
// name: "Alma" <= this works
// name: new RegExp(query, i ).toString()
// $or: [{ name: new RegExp(query, i ).toString() }],
// name: {pattern: .*a.* ,options: }
// name: { $eq: "Alma" }
}
});
// not assignable to type string | FindOperator<string> .ts(2322)
}
}
All of the commented solutions doesn t work and error I face is "not assignable to type string | FindOperator .ts(2322)" This is because in entity, name is of type string and offcourse it should be, this is my entity file
import {Entity, ObjectID, ObjectIdColumn, Column} from "typeorm";
@Entity()
export class City {
@ObjectIdColumn()
_id: ObjectID;
@Column()
id: string;
@Column()
name: string;
There is an option available for me to move to mongoose but I want to stick to typeorm due to its verstality for all type of dbs I am beginner in mongo and may be thats why I am missing some basics of it, but I am worked in typeorm before and have experience in it