不能将$OR运算符与 MongoDB 和 Typeorm 一起使用

Cannot use $OR operator with MongoDB and Typeorm

提问人:Kevin Temes 提问时间:11/7/2023 更新时间:11/7/2023 访问量:16

问:

经过几次尝试并阅读了大量文档,我仍然无法对需要 $or 运算符的 MongoDB 进行非常基本的 TypeOrm 查询。

这是我的实体:

import { Column, Entity, ObjectId, ObjectIdColumn } from 'typeorm';

@Entity({ name: 'collections' })
export class Book {
  // Global fields
  @ObjectIdColumn()
  _id: ObjectId;

  @Column()
  title: string;

  @Column()
  subtitle: string;

  @Column()
  collection_level: string;

  @Column()
  parent_collection_id: string;

  @Column()
  available: string;

  @Column()
  book_cover_img_url: string;

  @Column()
  short_id: number;

  @Column()
  book_id: string;

  @Column()
  book_title: string;

  @Column()
  img_dir: string;

  // collection level 1 fields

  @Column()
  product_code: string;

  @Column()
  edition: string;

  @Column()
  authors: string;

  @Column()
  cover_image: string;

  @Column()
  instructor_resources_url: string;

  @Column()
  nla_isbn: string;

  @Column()
  is_nla: string;

  @Column()
  show_in_dlt: string;
}

这是我迄今为止尝试过的替代方案:

  findBooksByDiscipline(parent: string): Promise<Book[]> {
    return this.BookRepository.find({
      where: [
        {
          collection_level: '1',
          parent_collection_id: parent,
          is_nla: 'false',
          show_in_dlt: 'true',
        },
        {
          collection_level: '1',
          parent_collection_id: parent,
          is_nla: IsNull(),
          show_in_dlt: 'true',
        },
      ],
    });
  }

第一个让我们应用程序初始化没有问题,但是在实际调用函数时,我收到此错误:MongoInvalidArgumentError: Query filter must be a plain object or ObjectId

以下尝试是:

  findBooksByDiscipline(parent: string): Promise<Book[]> {
    return this.BookRepository.find({
      where: {
        $or: [
          {
            collection_level: '1',
            parent_collection_id: parent,
            is_nla: 'false',
            show_in_dlt: 'true',
          },
          {
            collection_level: '1',
            parent_collection_id: parent,
            is_nla: IsNull(),
            show_in_dlt: 'true',
          },
        ]
      }
    });
  }

这个甚至不让应用程序启动。尝试启动应用程序时,出现以下错误:

error TS2322: Type '{ $or: ({ collection_level: string; parent_collection_id: string; is_nla: string; show_in_dlt: string; } | { collection_level: string; parent_collection_id: string; is_nla: FindOperator<any>; show_in_dlt: string; })[]; }' is not assignable to type 'FindOptionsWhere<Book> | FindOptionsWhere<Book>[] | undefined'. Object literal may only specify known properties, and '$or' does not exist in type 'FindOptionsWhere<Book> | FindOptionsWhere<Book>[]'.

最后一个选项是:

  findBooksByDiscipline(parent: string): Promise<Book[]> {
    return this.BookRepository.find({
      $or: [
          {
            collection_level: '1',
            parent_collection_id: parent,
            is_nla: 'false',
            show_in_dlt: 'true',
          },
          {
            collection_level: '1',
            parent_collection_id: parent,
            is_nla: IsNull(),
            show_in_dlt: 'true',
          },
        ]
    });
  }

与上一个相同,在尝试初始化应用程序时失败,并出现以下错误:

error TS2345: Argument of type '{ $or: ({ collection_level: string; parent_collection_id: string; is_nla: string; show_in_dlt: string; } | { collection_level: string; parent_collection_id: string; is_nla: FindOperator<any>; show_in_dlt: string; })[]; }' is not assignable to parameter of type 'FindManyOptions<Book>'. Object literal may only specify known properties, and '$or' does not exist in type 'FindManyOptions<Book>'.

这是我的依赖版本,以防万一:

 "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/swagger": "^6.0.0",
    "@nestjs/typeorm": "^9.0.0",
    "@types/luxon": "^1.26.5",
    "axios": "^0.21.4",
    "class-transformer": "^0.4.0",
    "class-validator": "^0.13.1",
    "cookie-parser": "^1.4.5",
    "helmet": "^5.0.2",
    "husky": "^4.3.8",
    "jsonwebtoken": "^9.0.0",
    "mongodb": "^5.2.0",
    "mongodb-uri": "^0.9.7",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.0.0",
    "swagger-ui-express": "^4.1.6",
    "typeorm": "^0.3.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/cookie-parser": "^1.4.2",
    "@types/express": "^4.17.8",
    "@types/jest": "^26.0.15",
    "@types/jsonwebtoken": "^8.5.9",
    "@types/node": "^14.14.6",
    "@types/supertest": "^2.0.10",
    "@types/validator": "^13.7.7",
    "@typescript-eslint/eslint-plugin": "^6.6.0",
    "@typescript-eslint/parser": "^6.6.0",
    "eslint": "^7.12.1",
    "eslint-config-prettier": "^6.15.0",
    "eslint-plugin-prettier": "^3.1.4",
    "jest": "^28.0.0",
    "prettier": "^2.1.2",
    "supertest": "^6.0.0",
    "ts-jest": "^28.0.0",
    "ts-loader": "^8.0.8",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^5.0.4"
  },
  "jest": {
    "preset": "./test-setup/jest.config-spec.json"
  }

关于问题可能是什么的任何线索?

提前致谢

MongoDB 类型

评论


答: 暂无答案