Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-3454

Projection<TSchema> type is too narrow in Node.js driver

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 4.1.1
    • Affects Version/s: None
    • Component/s: Native
    • 2
    • Not Needed

      What problem are you facing?

      Projection<T>'s definition seems to be too narrow; specifically, <field>: <expression> type support is missing even though a projection containing a string literal expression runs perfectly fine (all my unit tests still pass) and, if I'm reading it correctly, is described in the documentation as a supported feature.

      Here is the offending code from my current project:

      Unable to find source-code formatter for language: typescript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      const publicMemeProjection = {
        _id: false,
        meme_id: { $toString: '$_id' },
        owner: { $toString: '$owner' },
        receiver: { $toString: '$receiver' },
        createdAt: true,
        expiredAt: true,
        description: true,
        likes: '$totalLikes', // <== cause of TS2345 error
        private: true,
        replyTo: { $toString: '$replyTo' },
        imageUrl: true
      };
      
      ...
      
      const memes = await (await getDb())
        .collection<InternalMeme>('memes')
        .find({ _id: { $in: meme_ids } })
        .sort({ _id: -1 })
        .limit(getEnv().RESULTS_PER_PAGE)
        .project<PublicMeme>(publicMemeProjection) // <== location of TS2345 error
        .toArray();
      

      Which causes the following error:

      error TS2345: Argument of type '{ _id: boolean; meme_id: { $toString: string; }; owner: { $toString: string; }; receiver: { $toString: string; }; createdAt: boolean; expiredAt: boolean; description: boolean; likes: string; private: boolean; replyTo: { ...; }; imageUrl: boolean; }' is not assignable to parameter of type 'Projection<PublicMeme>'.
        Type '{ _id: boolean; meme_id: { $toString: string; }; owner: { $toString: string; }; receiver: { $toString: string; }; createdAt: boolean; expiredAt: boolean; description: boolean; likes: string; private: boolean; replyTo: { ...; }; imageUrl: boolean; }' is not assignable to type '{ createdAt?: boolean | 0 | 1 | ProjectionOperators | undefined; expiredAt?: boolean | 0 | 1 | ProjectionOperators | undefined; description?: boolean | ... 3 more ... | undefined; ... 6 more ...; likes?: boolean | ... 3 more ... | undefined; }'.
          Types of property 'likes' are incompatible.
            Type 'string' is not assignable to type 'boolean | 0 | 1 | ProjectionOperators | undefined'.
      
      ==>       .project<PublicMeme>(publicMemeProjection)
      

      Related: @types/mongodb doesn't type the argument passed to project().

      What driver and relevant dependency versions are you using?

      • Node.js native driver mongodb@4.0.0
      • Using latest MongoDb 4.x
      • Previously used @types/mongodb

      Steps to reproduce?

      See above.

            Assignee:
            neal.beeken@mongodb.com Neal Beeken
            Reporter:
            git@ergodark.com Bernard Dickens
            Daria Pardue
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: