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

Type error with $addToSet in bulkWrite

    • 2
    • 0
    • Not Needed
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      If an index signature is used when defining a document interface, an error will be reported when using addToSet in the bulkWrite method.

      But if addToSet is used in the updateOne method, or addToSet in the bulkWrite method is used in an interface Without index signature, this will not happen. The former (the addToSet in the updateOne) additionally results in no restrictions on field types due to the index signature.

       

      Code Snippet:

       

      import type { Collection, Document } from 'mongodb';
      
      interface TestDocument {
        readonly myId: number;
        readonly mySet: number[];
      }
      const collection = undefined as unknown as Collection<TestDocument>;
      collection.updateOne({ myId: 0 }, { $addToSet: { mySet: 0 } });
      collection.bulkWrite([
        {
          updateOne: {
            filter: { myId: 0 },
            update: {
              $addToSet: { mySet: 0 },
            },
          },
        },
      ]);
      
      interface IndexSingatureTestDocument extends Document {
        readonly myId: number;
        readonly mySet: number[];
      }
      const indexSingatureCollection = undefined as unknown as Collection<IndexSingatureTestDocument>;
      indexSingatureCollection.updateOne({ myId: 0 }, { $addToSet: { mySet: '' } });
      indexSingatureCollection.bulkWrite([
        {
          updateOne: {
            filter: { myId: 0 },
            update: {
              $addToSet: { mySet: 0 },  // 
            },
          },
        },
      ]);

       

       

      My TypeScript version is 5.1.3.

      I tried both >=6.1.0 and >=5.7.0 versions of mongodb, and it reproduced stably.

      Acceptance Criteria

      • The code sample from the ticket description should not show errors for:
        • Collection.updateOne()
        • Collection.updateMany()
        • bulkWrite.updateOne
        • bulkWrite.updateMany
          • Change the type of `update` parameter to be `UpdateFilter<TSchema> | Document[]`
          • Cover those cases with type tests
      • The code sample from the NODE-4664 should not show errors for the same methods listed above.
      • Fields of any type should not show errors.
      • Add API comments explaining that:
        • `UpdateFilter<TSchema>` covers a document that contains update operator expressions
        • `Document[]` covers aggregation pipelines in the update filter
      • Verify if documentation needs to be updated.

            Assignee:
            alena.khineika@mongodb.com Alena Khineika
            Reporter:
            RollingSnack831@gmail.com Snack N/A
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: