question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
andreialecucommented, Jun 20, 2022

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'.
0reactions
vkarpov15commented, Aug 11, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Type 'ObjectId' is not assignable to type 'Expression' in ...
guys when i want to aggregate in mongoDB using typescript i got this error. Type 'ObjectId' is not assignable to type 'Expression'.
Read more >
Aggregation pipeline typing broke after update cannot match ...
Type 'ObjectId' is not assignable to type 'Expression'. If I remove the cast, it does not work, but typing error goes away.
Read more >
Type 'ObjectId' is not assignable to type 'never' - MongoDB
When i use non-array functions like $find etc. it works fine. Do i have to type every Collection? How would it work with...
Read more >
Mongoose v6.8.2: API docs
Used for declaring paths in your schema that should be MongoDB ObjectIds. Do not use this to create a new ObjectId instance, use...
Read more >
MongoDB Typescript Error "Type 'ObjectId' is not assignable to ...
Coding example for the question MongoDB Typescript Error "Type 'ObjectId' is not assignable to type 'never'-mongodb.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found