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

Bson serialisation throw error on inner keys starting with '$'

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Blocker - P1
    • Resolution: Won't Fix
    • Affects Version/s: 3.1.3, 3.1.4
    • Fix Version/s: None
    • Component/s: BSON
    • Labels:
    • Environment:
      OS: macOS 10.13.6 (17G65)
      Node.js: v10.5.0

      Description

      Code to reproduce:

      const MongoClient = require("mongodb").MongoClient;
       
      const inner$ = { a: { $b: 1 } };
       
      MongoClient.connect(
        "mongodb://localhost:27017",
        { useNewUrlParser: true }
      )
        .then(client => client.db("db").collection("col"))
        .then(collection => collection.insertOne(inner$))
        .then(console.log)
        .catch(console.error)
        .then(() => process.exit());
      
      

      Expected:

      Document inserted, as specified in field limitations official documentation :

      Field names can contain dots (i.e. .) and dollar signs (i.e. $). However, only nested field names may start with a dollar sign. For example, attempting to insert { "a" : { "$b": 1 } } will succeed but attempting to insert {"$a": 1} results in error.

      Actual

      Throws error:

      Error: key $b must not start with '$'
          at serializeInto (/Users/barata/Documents/test/npm/node_modules/bson/lib/bson/parser/serializer.js:912:19)
          at serializeObject (/Users/barata/Documents/test/npm/node_modules/bson/lib/bson/parser/serializer.js:348:18)
          at serializeInto (/Users/barata/Documents/test/npm/node_modules/bson/lib/bson/parser/serializer.js:938:17)
          at serializeObject (/Users/barata/Documents/test/npm/node_modules/bson/lib/bson/parser/serializer.js:348:18)
          at serializeInto (/Users/barata/Documents/test/npm/node_modules/bson/lib/bson/parser/serializer.js:728:17)
          at serializeObject (/Users/barata/Documents/test/npm/node_modules/bson/lib/bson/parser/serializer.js:348:18)
          at serializeInto (/Users/barata/Documents/test/npm/node_modules/bson/lib/bson/parser/serializer.js:938:17)
          at BSON.serialize (/Users/barata/Documents/test/npm/node_modules/bson/lib/bson/bson.js:63:28)
          at Query.toBin (/Users/barata/Documents/test/npm/node_modules/mongodb-core/lib/connection/commands.js:146:25)
          at serializeCommands (/Users/barata/Documents/test/npm/node_modules/mongodb-core/lib/connection/pool.js:1045:43)
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              katherine.walker Katherine Walker (Inactive)
              Reporter:
              Banst Bastien Arata
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: