使用 $in mikro orm 的类型问题 mongodb
Type issues on using $in mikro orm mongodb
我正在为我的项目使用带有 mongodb 的 mikro orm。到目前为止,它是打字稿中 MongoDB 的最佳形式。但是我在将 $in 与对象 ID 一起使用时遇到类型错误。这是我的代码:
@Query(() => [Post])
async allPosts(@Ctx() { em }: appContext) {
const posts = await em.find(Post, {});
const repo = em.getRepository(Post)
const multiplePosts = await repo.find({ _id: { $in: [new ObjectId("6005d0c253e2b72af07fc61a")] } });
console.log("multiplePosts: ", multiplePosts);
return posts;
}
Post.ts
export class Post {
@Field(() => ID)
@PrimaryKey()
_id!: string;
@Field(() => String)
@Property()
createdAt = new Date();
@Field(() => String)
@Property({ onUpdate: () => new Date() })
updatedAt = new Date();
@Field(() => String)
@Property()
title!: string;
@Field(() => String)
@Property()
excerpt!: string;
@Field(() => String)
@Property()
content!: string;
@Field()
@ManyToOne()
author!: User;
}
这是我遇到的错误:
src/resolvers/PostResolver.ts:32:43 - error TS2769: No overload matches this call.
Overload 1 of 2, '(where: FilterQuery<Post>, options?: FindOptions<Post, any> | undefined): Promise<Post[]>', gave the following error.
Argument of type '{ _id: { $in: ObjectId[]; }; }' is not assignable to parameter of type 'FilterQuery<Post>'.
Types of property '_id' are incompatible.
Type '{ $in: ObjectId[]; }' is not assignable to type 'string | RegExp | OperatorMap<FilterValue2<string>> | FilterValue2<string>[] |n
ull | undefined'.
Types of property '$in' are incompatible.
Type 'ObjectId[]' is not assignable to type 'FilterValue2<string>[]'.
Type 'ObjectId' is not assignable to type 'FilterValue2<string>'.
Type 'ObjectId' is missing the following properties from type 'RegExp': exec, test, source, global, and 13 more.
Overload 2 of 2, '(where: FilterQuery<Post>, populate?: any, orderBy?: QueryOrderMap | undefined, limit?: number | undefined, offset?: number | undefined): Promise<...>', gave the following error.
Argument of type '{ _id: { $in: ObjectId[]; }; }' is not assignable to parameter of type 'FilterQuery<Post>'.
Types of property '_id' are incompatible.
Type '{ $in: ObjectId[]; }' is not assignable to type 'string | RegExp | OperatorMap<FilterValue2<string>> | FilterValue2<string>[] | null | undefined'.
Types of property '$in' are incompatible.
Type 'ObjectId[]' is not assignable to type 'FilterValue2<string>[]'.
32 const multiplePosts = await repo.find({
~
33 _id: { $in: [new ObjectId("6005d0c253e2b72af07fc61a")] },
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34 });
~~~~~
at createTSError (D:\blog\server\node_modules\ts-node\src\index.ts:513:12)
at reportTSError (D:\blog\server\node_modules\ts-node\src\index.ts:517:19)
at getOutput (D:\blog\server\node_modules\ts-node\src\index.ts:752:36)
at Object.compile (D:\blog\server\node_modules\ts-node\src\index.ts:968:32)
at Module.m._compile (D:\blog\server\node_modules\ts-node\src\index.ts:1056:42)
at Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Object.require.extensions.<computed> [as .ts] (D:\blog\server\node_modules\ts-node\src\index.ts:1059:12)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:997:19)
删除 ObjectId 让打字稿编译器满意,但 MongoDB 不会将它们视为对象 ID。
已编辑:此外,添加 //ts-ignore
效果很好,所以这是一个类型问题。
问题出在您的实体定义中,您明确指出 _id
类型是 string
,而不是 ObjectId
。所以如果是object id,修正定义:
@PrimaryKey()
_id!: ObjectId;
那么您的查询应该通过了。请注意,您还可以定义序列化 PK id: string
,它将充当 ObjectId 字符串版本的虚拟 getter。
https://mikro-orm.io/docs/usage-with-mongo/
You might need to install @types/mongodb
.
一般属性类型如果是ObjectId,也可以通过字符串形式查询。这些应该相等:
repo.find({ _id: { $in: [new ObjectId("6005d0c253e2b72af07fc61a")] } });
repo.find({ _id: { $in: ["6005d0c253e2b72af07fc61a"] } });
repo.find({ _id: [new ObjectId("6005d0c253e2b72af07fc61a")] });
repo.find({ _id: ["6005d0c253e2b72af07fc61a"] });
repo.find([new ObjectId("6005d0c253e2b72af07fc61a")]);
repo.find(["6005d0c253e2b72af07fc61a"]);
另请注意,除非您使用 ts-morph,否则您应该在使用 属性 初始值设定项的地方放置显式类型:
@Property()
createdAt: Date = new Date();
https://mikro-orm.io/docs/metadata-providers/#limitations-and-requirements
我正在为我的项目使用带有 mongodb 的 mikro orm。到目前为止,它是打字稿中 MongoDB 的最佳形式。但是我在将 $in 与对象 ID 一起使用时遇到类型错误。这是我的代码:
@Query(() => [Post])
async allPosts(@Ctx() { em }: appContext) {
const posts = await em.find(Post, {});
const repo = em.getRepository(Post)
const multiplePosts = await repo.find({ _id: { $in: [new ObjectId("6005d0c253e2b72af07fc61a")] } });
console.log("multiplePosts: ", multiplePosts);
return posts;
}
Post.ts
export class Post {
@Field(() => ID)
@PrimaryKey()
_id!: string;
@Field(() => String)
@Property()
createdAt = new Date();
@Field(() => String)
@Property({ onUpdate: () => new Date() })
updatedAt = new Date();
@Field(() => String)
@Property()
title!: string;
@Field(() => String)
@Property()
excerpt!: string;
@Field(() => String)
@Property()
content!: string;
@Field()
@ManyToOne()
author!: User;
}
这是我遇到的错误:
src/resolvers/PostResolver.ts:32:43 - error TS2769: No overload matches this call.
Overload 1 of 2, '(where: FilterQuery<Post>, options?: FindOptions<Post, any> | undefined): Promise<Post[]>', gave the following error.
Argument of type '{ _id: { $in: ObjectId[]; }; }' is not assignable to parameter of type 'FilterQuery<Post>'.
Types of property '_id' are incompatible.
Type '{ $in: ObjectId[]; }' is not assignable to type 'string | RegExp | OperatorMap<FilterValue2<string>> | FilterValue2<string>[] |n
ull | undefined'.
Types of property '$in' are incompatible.
Type 'ObjectId[]' is not assignable to type 'FilterValue2<string>[]'.
Type 'ObjectId' is not assignable to type 'FilterValue2<string>'.
Type 'ObjectId' is missing the following properties from type 'RegExp': exec, test, source, global, and 13 more.
Overload 2 of 2, '(where: FilterQuery<Post>, populate?: any, orderBy?: QueryOrderMap | undefined, limit?: number | undefined, offset?: number | undefined): Promise<...>', gave the following error.
Argument of type '{ _id: { $in: ObjectId[]; }; }' is not assignable to parameter of type 'FilterQuery<Post>'.
Types of property '_id' are incompatible.
Type '{ $in: ObjectId[]; }' is not assignable to type 'string | RegExp | OperatorMap<FilterValue2<string>> | FilterValue2<string>[] | null | undefined'.
Types of property '$in' are incompatible.
Type 'ObjectId[]' is not assignable to type 'FilterValue2<string>[]'.
32 const multiplePosts = await repo.find({
~
33 _id: { $in: [new ObjectId("6005d0c253e2b72af07fc61a")] },
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34 });
~~~~~
at createTSError (D:\blog\server\node_modules\ts-node\src\index.ts:513:12)
at reportTSError (D:\blog\server\node_modules\ts-node\src\index.ts:517:19)
at getOutput (D:\blog\server\node_modules\ts-node\src\index.ts:752:36)
at Object.compile (D:\blog\server\node_modules\ts-node\src\index.ts:968:32)
at Module.m._compile (D:\blog\server\node_modules\ts-node\src\index.ts:1056:42)
at Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Object.require.extensions.<computed> [as .ts] (D:\blog\server\node_modules\ts-node\src\index.ts:1059:12)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:997:19)
删除 ObjectId 让打字稿编译器满意,但 MongoDB 不会将它们视为对象 ID。
已编辑:此外,添加 //ts-ignore
效果很好,所以这是一个类型问题。
问题出在您的实体定义中,您明确指出 _id
类型是 string
,而不是 ObjectId
。所以如果是object id,修正定义:
@PrimaryKey()
_id!: ObjectId;
那么您的查询应该通过了。请注意,您还可以定义序列化 PK id: string
,它将充当 ObjectId 字符串版本的虚拟 getter。
https://mikro-orm.io/docs/usage-with-mongo/
You might need to install
@types/mongodb
.
一般属性类型如果是ObjectId,也可以通过字符串形式查询。这些应该相等:
repo.find({ _id: { $in: [new ObjectId("6005d0c253e2b72af07fc61a")] } });
repo.find({ _id: { $in: ["6005d0c253e2b72af07fc61a"] } });
repo.find({ _id: [new ObjectId("6005d0c253e2b72af07fc61a")] });
repo.find({ _id: ["6005d0c253e2b72af07fc61a"] });
repo.find([new ObjectId("6005d0c253e2b72af07fc61a")]);
repo.find(["6005d0c253e2b72af07fc61a"]);
另请注意,除非您使用 ts-morph,否则您应该在使用 属性 初始值设定项的地方放置显式类型:
@Property()
createdAt: Date = new Date();
https://mikro-orm.io/docs/metadata-providers/#limitations-and-requirements