Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aggregate $match Error: Type 'ObjectId' is not assignable to type 'Expression'. #11952

Closed
2 tasks done
sm-hejazi opened this issue Jun 19, 2022 · 5 comments · Fixed by #11956
Closed
2 tasks done

aggregate $match Error: Type 'ObjectId' is not assignable to type 'Expression'. #11952

sm-hejazi opened this issue Jun 19, 2022 · 5 comments · Fixed by #11956
Labels
typescript Types or Types-test related issue / Pull Request

Comments

@sm-hejazi
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.4.0

Node.js version

16.14.0

MongoDB server version

5.0.9

Description

update to latest version and get error on aggregate

  await Model.aggregate([
                  {
                      $match: { _id: new mongoose.Types.ObjectId('stringObjecId') },
                  },
  ]);

get Error Type 'ObjectId' is not assignable to type 'Expression'.
how to solve this?

Steps to Reproduce

  await Model.aggregate([
                  {
                      $match: { _id: new mongoose.Types.ObjectId('stringObjecId') },
                  },
  ]);

Expected Behavior

No response

@andreialecu
Copy link
Contributor

I think you can work around it with {$match: { _id: {$eq: new mongoose.Types.ObjectId('stringObjecId')} }, but this seems like a bug with the types.

I'm also seeing an error with something like:
Screenshot 2022-06-20 at 11 09 06

According to MongoDB docs, you can compare with null to match either non-existant or null values, but the types do not seem to allow this.

@andreialecu
Copy link
Contributor

Found more things that are being broken by the types. All 3 of these here:

Screenshot 2022-06-20 at 11 15 21

Errors:

Type '{ $ifNull: string[]; }' is not assignable to type 'AnyExpression'.
      Object literal may only specify known properties, and '$ifNull' does not exist in type 'any[] | NativeDate | ArrayElemAt | First | Last | Reduce | ConcatArrays | Filter | Map | ObjectToArray | ... 118 more ... | ToObjectId'.
Type '{ $exists: true; }' is not assignable to type 'Expression'.
      Object literal may only specify known properties, and '$exists' does not exist in type 'ArrayElemAt | First | Last | Reduce | ObjectToArray | RegexFindAll | Split | SetDifference | ... 132 more ... | Rank'.
Overload 2 of 2, '(pipeline: PipelineStage[], callback?: Callback<{ type: string; language: string; deviceTokens: string[]; }[]> | undefined): Aggregate<{ type: string; language: string; deviceTokens: string[]; }[]>', gave the following error.
    Type 'null' is not assignable to type 'Expression'.

@andreialecu
Copy link
Contributor

I opened #11956, which should fix this.

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jun 21, 2022
@jyeros
Copy link
Contributor

jyeros commented Jun 27, 2022

I am having a similar issue, I am trying $ne: ['$$indexNotNull', null], but types doesn't allow null as a second parameter. A fix could be add null as second parameter with an union type. But, I think null is a literal expression in mongo.

@vkarpov15 vkarpov15 reopened this Jul 26, 2022
@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Jul 26, 2022
@vkarpov15 vkarpov15 added this to the 6.5.3 milestone Jul 26, 2022
@vkarpov15
Copy link
Collaborator

@jyeros this should be fixed in 6.4.3 with 9f3f8eb .

@vkarpov15 vkarpov15 removed this from the 6.5.3 milestone Aug 11, 2022
@Automattic Automattic locked as resolved and limited conversation to collaborators Aug 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants