English 中文(简体)
查阅文件
原标题:Search in Documents Not Working For Typeorm, mongodb, nestjs

我有 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

问题回答

如果在互联网上发现同样的问题,那么我就作出了非常令人信服的解决办法。

const ugly: any = { $regex: /a/ }
 return this.citiesRepository.find({
  where: {
    name: ugly
  }
});

我做了一些测试,发现它非常类似于ongo。

给我的工作

const ugly: any = { $regex: /a/ }
 return this.citiesRepository.find({
  where: {
    name: {
      $regex: new RegExp( a ),
      $options:  i ,
    },
});




相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签