Skip to content

Conversation

@lerouxb
Copy link
Contributor

@lerouxb lerouxb commented Nov 20, 2025

MONGOSH-1285

I built this on top of @addaleax's wip branch, so the types are still mostly from there.


The short version of what we're trying to do: For BSON that we get from the database we want to print all of it, untruncated, when it gets evaluated or otherwise inspected with util.inspect().


This solution wraps the ServiceProvider with another class that implements all the same methods. Then:

  • all methods that return cursors have the returned cursors intercepted and wrapped so that the relevant methods on there that return documents are replaced with new methods that recursively install our inspect function.
  • all methods that return promises of bson have their results intercepted and we recursively install our inspect function on the result.
  • all other methods are just forwarded with their results unchanged.

I have been testing it with this document which should be affected by the inspectOptions depth, maxArrayLength and maxStringLength:

db.test.insertOne({
  array: Array.from(Array(1000), (_,i) => i),
  string: 'All work and no play makes Jack a dull boy. '.repeat(250),
  object: {
    foo: {
      bar: {
        baz: {
          qux: {
            quux: {
              corge: {
                grault: 'If you can read this, you are too close.'
              }
            }
          }
        }
      }
    }
  }
});

And this doc has every BSON type which is useful for testing that we're not messing up existing BSON formatting:

db.test.insertOne({
    double: Double(1.2),
    doubleThatIsAlsoAnInteger: Double(1),
    string: 'Hello, world!',
    binData: Binary(Buffer.from([1, 2, 3])),
    boolean: true,
    date: Date('2023-04-05T13:25:08.445Z'),
    null: null,
    regex: BSONRegExp('pattern', 'i'),
    javascript: Code('function() {}'),
    symbol: BSONSymbol('symbol'),
    javascriptWithScope: Code('function() {}', { foo: 1, bar: 'a' }),
    int: Int32(12345),
    timestamp: Timestamp(Long('7218556297505931265')),
    long: Long('123456789123456789'),
    decimal: Decimal128(
      Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
    ),
    minKey: MinKey(),
    maxKey: MaxKey(),

    binaries: {
      generic: Binary(Buffer.from([1, 2, 3]), 0),
      functionData: Binary(Buffer.from('//8='), 1),
      binaryOld: Binary(Buffer.from('//8='), 2),
      uuidOld: Binary(Buffer.from('c//SZESzTGmQ6OfR38A11A=='), 3),
      uuid: UUID('AAAAAAAA-AAAA-4AAA-AAAA-AAAAAAAAAAAA'),
      md5: Binary(Buffer.from('c//SZESzTGmQ6OfR38A11A=='), 5),
      encrypted: Binary(Buffer.from('c//SZESzTGmQ6OfR38A11A=='), 6),
      compressedTimeSeries: Binary(
        Buffer.from(
          'CQCKW/8XjAEAAIfx//////////H/////////AQAAAAAAAABfAAAAAAAAAAEAAAAAAAAAAgAAAAAAAAAHAAAAAAAAAA4AAAAAAAAAAA==',
          'base64'
        ),
      ),
      custom: Binary(Buffer.from('//8='), 128),
    },

    dbRef: DBRef('namespace', ObjectId('642d76b4b7ebfab15d3c4a78')),
  });

You can test that the find cursor's tryNext was replaced by running:

> db.test.find()

Notice that the whole array, string and object all printed. However, this specific case already worked before because we special-case printing cursors and a few other things.

This will exercise the inspect function on the top-level array that was returned.

You can test that it recursively installed it on the documents inside that array by running:

> db.test.find().toArray()[0]

or even

> config.set('inspectDepth', 2);
> f = { a: { b: { c: { d: 1 } } } }
{ a: { b: { c: [Object] } } } # notice that it truncated
> db.test.find().toArray()[0].object # this will print the whole object, not just 2 levels

This needs a lot of tests. There are almost certainly some cases left that I've missed. And I'm unsure about some details. Just opening to have a discussion. Oh and we might want a way for users to opt out of it.

this: DeepInspectServiceProviderWrapper,
...args: Parameters<Required<ServiceProvider>[K]>
): // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore The returntype already contains a promise
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A way around all this is to just make one function similar to bsonMethod() for each unique return type, kinda like what I did for the cursor methods.

undefined,
undefined,
initialServiceProvider
this.initialServiceProvider
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw it is VERY easy to accidentally pass initialServiceProvider (ie. the unwrapped value) to something in place of this.initialServiceProvider. Ask me how I know..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants