I am using mikro orm with mongodb for my project. So far it's the best orm for MongoDB in typescript. But I am facing type errors on using $in with Object Id. Here's my code:
@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;
}
Here's the error I am getting:
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)
Removing the ObjectId makes the typescript compiler happy, but then MongoDB is not treating them as Object ID.
Edited: Also, adding //ts-ignore
works fine, so it's a type issue.
The issue comes from your entity definition, where you are explicitly saying that the _id
type is string
, not ObjectId
. So if it is an object id, fix the definition:
@PrimaryKey()
_id!: ObjectId;
Then your query should pass. Note that you can also define serialized PK id: string
that will act as a virtual getter for the string version of the ObjectId.
https://mikro-orm.io/docs/usage-with-mongo/
You might need to install
@types/mongodb
.
In general if the property type is ObjectId, it is possible to query by string form too. Those should be equal:
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"]);
Also note that unless you are using ts-morph, you should be putting explicit types where you use property initializer:
@Property()
createdAt: Date = new Date();
https://mikro-orm.io/docs/metadata-providers/#limitations-and-requirements