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

Return error when failing to serialize type

    XMLWordPrintable

Details

    • New Feature
    • Status: Closed
    • Major - P3
    • Resolution: Fixed
    • bson-1.1.5
    • bson-1.1.6
    • BSON
    • None

    Description

      The customer wanted to use BigInt in a document. We explained that the type is not supported, and gave him a workaround, which appears to work.

      SF ticket is https://support.mongodb.com/case/00619212

      However, customer says that the driver silently fails.

      `Not giving an error and not saving the document are serious bugs. You can see this behavior by executing the following code (without the toBSON, only 1 record is saved and retrived; with the .toBSON , 3 records are saved and retrieved; no error is reported in either case)`

      MongoDB Nodejs Driver v3.3.3
      Nodejs v 10.15.3
      MongoDB v 4.0.10

      const { MongoClient } = require('mongodb');
       
      run().catch(error => console.error(error.stack));
       
      class Datum {
          id : BigInt;
          stationName : string;
          totalDocks: number;
        };
        
        const SSData: Datum[] = 
        [
          {
              "id" : BigInt(3898),
              "stationName": "E 63 St & 3 Ave",
              "totalDocks": 57
            },
          { 
            "id": BigInt(3899),
            "stationName": "Manhattan Av & Leonard St",
            "totalDocks": 18
          },
          {
            "id": BigInt(864691128455137547),
            "stationName": "Cypress Ave & Palmetto St",
            "totalDocks": 25,
          }
        
        ];
        
      async function run() {
        const client = await MongoClient.connect('mongodb://localhost:27017/test', {
          useNewUrlParser: true
        });
       
        const db = client.db();
        await db.dropDatabase();
       
      // Add a `toBSON()` function to enable MongoDB to store BigInts as strings
      BigInt.prototype.toBSON = function() { return this.toString(); };
       
      for (let i = 0; i < SSData.length; i++) {
          try {
            await db.collection('BigInt').update({id:SSData[i].id}, SSData[i], {upsert: true});
          }
          catch (e){
              console.error(e.toString());
          }
      }
       
      console.log('All records persisted'); 
        let recno = 0;
        try {
        const res = await db.collection('BigInt').find({});
        res.forEach(function(doc) {
          console.log('Document ' + ++recno + '. ' + JSON.stringify(doc))
        }
        )
        }
        catch(e) {
          console.error('Error in find ' + e.toString());
        }
      }
      

      Attachments

        Issue Links

          Activity

            People

              neal.beeken@mongodb.com Neal Beeken
              jorge.imperial@mongodb.com Jorge Imperial-Sosa
              Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: