seppevs/migrate-mongo

Assigning an `_id` field with a valid mongodb ObjectID value gives a BSON version error

theRocket opened this issue · 4 comments

Describe the bug
Cannot assign an _id field a mongodb ObjectID value. Receive BSON version error. Code below produces:

> npx migrate-mongo up

ERROR: Could not migrate up 20230404223533-seed_accounts.js: Unsupported BSON version, bson types must be from bson 5.0 or later BSONVersionError: Unsupported BSON version, bson types must be from bson 5.0 or later
    at serializeInto (~/.npm/_npx/d044f46583e57438/node_modules/bson/lib/bson.cjs:3637:23)
    at serializeObject (~/.npm/_npx/d044f46583e57438/node_modules/bson/lib/bson.cjs:3199:22)
    at serializeInto (~/.npm/_npx/d044f46583e57438/node_modules/bson/lib/bson.cjs:3432:25)
    at serializeObject (~/.npm/_npx/d044f46583e57438/node_modules/bson/lib/bson.cjs:3199:22)
    at serializeInto (~/.npm/_npx/d044f46583e57438/node_modules/bson/lib/bson.cjs:3633:25)
    at Object.serialize (~/.npm/_npx/d044f46583e57438/node_modules/bson/lib/bson.cjs:4015:32)
    at Msg.serializeBson (~/.npm/_npx/d044f46583e57438/node_modules/mongodb/lib/cmap/commands.js:383:21)
    at Msg.makeDocumentSegment (~/.npm/_npx/d044f46583e57438/node_modules/mongodb/lib/cmap/commands.js:377:37)
    at Msg.toBin (~/.npm/_npx/d044f46583e57438/node_modules/mongodb/lib/cmap/commands.js:366:29)
    at MessageStream.writeCommand (~/.npm/_npx/d044f46583e57438/node_modules/mongodb/lib/cmap/message_stream.js:43:34)

Likely source of the error thrown:
https://github.com/mongodb/js-bson/blob/2f5d919a11f7817ef1f03b3de7a0bde2182cb2b0/src/parser/serializer.ts#L713

To Reproduce
Using a valid ObjectID string value from MongoDB itself:

const ObjectId = require('mongodb').ObjectId
const _id = new ObjectId('6452aa310a837f409e96e2e8')

module.exports = {
  async up (db, client) {
    const session = client.startSession()
    try {
      await session.withTransaction(async () => {
        const newAccount = {
          _id,
          name: 'Test Data'
        }
        const result = await db.collection('accounts').insertOne(newAccount)
        console.log('Seeded account:', result)
      })
    } finally {
      await session.endSession()
    }
  },

  async down (db, client) {
    const session = client.startSession()
    try {
      await session.withTransaction(async () => {
        await db.collection('accounts').deleteOne({ _id })
      })
    } finally {
      await session.endSession()
    }
  }
}

Removing the _id field gives us:

Seeded account: {
  acknowledged: true,
  insertedId: new ObjectId("6452ac3bb38f7175607d54d1")
}
MIGRATED UP: 20230404223533-seed_accounts.js

Expected behavior
Should be able to provide a valid BSON string value and assign the _id directly.

Additional context
It used to work based on #119 (comment)

In the mongosh environment, I can do this:

test> ob = ObjectId("6452aa310a837f409e96e2e8")
test> ob.getTimestamp()
ISODate("2023-05-03T18:38:41.000Z")

My local npx env reveals:

cat ~/.npm/_npx/d044f46583e57438/package-lock.json
...
    "migrate-mongo": {
      "version": "10.0.0",
      "resolved": "https://registry.npmjs.org/migrate-mongo/-/migrate-mongo-10.0.0.tgz",
      "integrity": "sha512-QA/bBKNMq/FmuK3fDbgwfNoW2riiU1wLDWGXv/tGhUItPLGqcciPPmu29SrnYAzRKMOVaGEXxzmrBs1zp5cQ7Q==",
      "requires": {
        "cli-table3": "^0.6.1",
        "commander": "^9.1.0",
        "date-fns": "^2.28.0",
        "fn-args": "^5.0.0",
        "fs-extra": "^10.0.1",
        "lodash": "^4.17.21",
        "p-each-series": "^2.2.0"
      }
    },
    "mongodb": {
      "version": "5.3.0",
      "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.3.0.tgz",
      "integrity": "sha512-Wy/sbahguL8c3TXQWXmuBabiLD+iVmz+tOgQf+FwkCjhUIorqbAxRbbz00g4ZoN4sXIPwpAlTANMaGRjGGTikQ==",
      "peer": true,
      "requires": {
        "bson": "^5.2.0",
        "mongodb-connection-string-url": "^2.6.0",
        "saslprep": "^1.0.3",
        "socks": "^2.7.1"
      }
    },

The BSON version shown matches the major version required, so I do not understand the error.

My package-lock.json reveals:

"node_modules/migrate-mongo": {
      "version": "9.0.0",
      "resolved": "https://registry.npmjs.org/migrate-mongo/-/migrate-mongo-9.0.0.tgz",
      "integrity": "sha512-Hs+5kmNdYtKo5574pvIxRAgHUkWtoXE0pIC5QPMCY1m7kwUVViuCtvOZQo0rd9rirtbI9+WDpYtI5ceG32Prgw==",
      "dependencies": {
        "cli-table3": "^0.6.1",
        "commander": "^9.1.0",
        "date-fns": "^2.28.0",
        "fn-args": "^5.0.0",
        "fs-extra": "^10.0.1",
        "lodash": "^4.17.21",
        "mongodb": "^4.4.1",
        "p-each-series": "^2.2.0"
      },
      "bin": {
        "migrate-mongo": "bin/migrate-mongo.js"
      },
     ...
 "mongodb": {
      "version": "4.11.0",
      "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-4.11.0.tgz",
      "integrity": "sha512-9l9n4Nk2BYZzljW3vHah3Z0rfS5npKw6ktnkmFgTcnzaXH1DRm3pDl6VMHu84EVb1lzmSaJC4OzWZqTkB5i2wg==",
      "requires": {
        "@aws-sdk/credential-providers": "^3.186.0",
        "bson": "^4.7.0",
        "denque": "^2.1.0",
        "mongodb-connection-string-url": "^2.5.4",
        "saslprep": "^1.0.3",
        "socks": "^2.7.1"
      }
...
    "bson": {
      "version": "4.7.0",
      "resolved": "https://registry.npmjs.org/bson/-/bson-4.7.0.tgz",
      "integrity": "sha512-VrlEE4vuiO1WTpfof4VmaVolCVYkYTgB9iWgYNOrVlnifpME/06fhFRmONgBhClD5pFC1t9ZWqFUQEQAzY43bA==",
      "requires": {
        "buffer": "^5.6.0"
      },

So perhaps this version mixing is the problem?

Even though our migrate-mongo command is this in package.json:

"migrate:up": "npx migrate-mongo up",

I had to bump the project dependencies like so:

  "dependencies": {
    ...
    "bson": "^5.2.0",
    ...
    "migrate-mongo": "^10.0.0",
    "mongodb": "^5.3.0",
    "mongoose": "^6.7.2"
  },

Then I could run the code above without errors.

This issue has been addressed in Mongoose with Milestone 6.11.6:
Automattic/mongoose#13712
and 7.4.0:
Automattic/mongoose#13515

So it's an issue with Mongoose, and not migrate-mongo?

Just confirming that this is still an issue.

Fixed by updating bson in my dependencies.