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

Feature request: Add support for projection operators #350

Open
CarlosLozanoHealthCaters opened this issue Oct 28, 2022 · 0 comments
Open

Comments

@CarlosLozanoHealthCaters
Copy link
Collaborator

Hey! MongoDB allows doing projections with operators in find queries (https://www.mongodb.com/docs/v6.0/reference/operator/projection/)

Example
Considering the following schema:

const userSchema = schema({
    firstName: types.string({ required: true }),
    lastName: types.string({ required: true }),
    grades: types.array(types.number())
});
const User = papr.model('users', userSchema);

And having the following info stored:

{
  firstName: 'John',
  lastName: 'Smith',
  grades: [9, 8, 10]
}

The following query:

const userInfo = await User.find({
    firstName: 'John'
}, {
    projection: {
        firstName: 1,
        grades: {$slice: 1}
    }
});

should output the following data:

{
  firstName: 'John',
  grades: [9]
}

Current issue
The current version force you to set a number in the projection field and doesn't allow you to include projection operators.

Probably we only need to change the following types:

  • export declare type ProjectionType<TSchema extends BaseSchema, Projection extends Partial<Record<Join<NestedPaths<WithId<TSchema>>, '.'>, number>> | undefined> = undefined extends Projection ? WithId<TSchema> : WithId<DeepPick<TSchema, '_id' | (keyof Projection & string)>>;
  • export declare type Projection<TSchema> = Partial<Record<Join<NestedPaths<WithId<TSchema>>, '.'>, number>>;

These types have a Record with values as numbers. I believe that it would be enough changing those Records, but I would like to get the community knowledge here.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant