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

fix(types): aggregate.addFields({score: { $meta: "textScore" } }) #12096

Closed
2 tasks done
herrerodani opened this issue Jul 12, 2022 · 1 comment
Closed
2 tasks done

fix(types): aggregate.addFields({score: { $meta: "textScore" } }) #12096

herrerodani opened this issue Jul 12, 2022 · 1 comment
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@herrerodani
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.4

Node.js version

17.7.1

MongoDB server version

5.0.9

Description

Can't compile with Typescript

Steps to Reproduce

aggregate.addFields({
score: {
$meta: "textScore"
}
})

gives the following typescript error:

TS2322: Type '{ $meta: string; }' is not assignable to type 'AnyExpression'.
Object literal may only specify known properties, and '$meta' does not exist in type 'any[] | NativeDate | ArrayElemAt | First | Last | Reduce | ConcatArrays | Filter | Map | ObjectToArray | ... 134 more ... | Switch'.

Expected Behavior

No response

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Jul 13, 2022
@IslandRhythms
Copy link
Collaborator

import * as mongoose from 'mongoose';

// 1. Create an interface representing a document in MongoDB.
interface IUser {
  name: string;
  email: string;
  avatar?: string;
}

// 2. Create a Schema corresponding to the document interface.
const userSchema = new mongoose.Schema<IUser>({
  name: { type: String, required: true },
  email: { type: String, required: true },
  avatar: String
});

// 3. Create a Model.
const User = mongoose.model<IUser>('User', userSchema);

run().catch(err => console.log(err));

async function run() {
  // 4. Connect to MongoDB
  await mongoose.connect('mongodb://localhost:27017/test');
  await mongoose.connection.dropDatabase();

  const user = new User({
    name: 'Bill',
    email: 'bill@initech.com',
    avatar: 'https://i.imgur.com/dM7Thhn.png'
  });
  await user.save();

  User.aggregate().addFields(
    { name: { $meta: "Bill" }}
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants