Mongo shell behaviour bug?

PS C:\Users\ma> mongo
MongoDB shell version v4.2.7
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f8fb8503-9db8-44f3-be51-81ebb7b0f0cd") }
MongoDB server version: 4.2.7
Server has startup warnings:
2020-06-07T16:24:10.577+0800 I  CONTROL  [initandlisten]
2020-06-07T16:24:10.578+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-06-07T16:24:10.578+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-06-07T16:24:10.582+0800 I  CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> Object.entries(this)
2020-06-20T16:23:42.059+0800 E  QUERY    [js] uncaught exception: Error: Unknown Error Code: tojson :
get@build/opt/mongo/shell/error_codes.js:35:23
tojsonObject@src/mongo/shell/types.js:657:9
tojson@src/mongo/shell/types.js:629:21
Array.tojson@src/mongo/shell/types.js:197:23
tojsonObject@src/mongo/shell/types.js:663:16
tojson@src/mongo/shell/types.js:629:21
Array.tojson@src/mongo/shell/types.js:197:23
tojsonObject@src/mongo/shell/types.js:663:16
tojson@src/mongo/shell/types.js:629:21
shellPrintHelper@src/mongo/shell/utils.js:636:15
@(shell2):1:1
> Object.getOwnPropertyDescriptors(this)
2020-06-20T16:23:43.389+0800 E  QUERY    [js] uncaught exception: Error: Unknown Error Code: tojson :
get@build/opt/mongo/shell/error_codes.js:35:23
tojsonObject@src/mongo/shell/types.js:657:9
tojson@src/mongo/shell/types.js:629:21
tojsonObject@src/mongo/shell/types.js:699:57
tojson@src/mongo/shell/types.js:629:21
tojsonObject@src/mongo/shell/types.js:699:57
tojson@src/mongo/shell/types.js:629:21
shellPrintHelper@src/mongo/shell/utils.js:636:15
@(shell2):1:1
>

Hi,

Unlike a browser or Node.js environment, the mongo shell does not have a default globally scoped object for this so your invocations aren’t expected to provide any meaningful results. In a browser environment the global this refers to the Window object and in Node the global object.

Your function calls will work as expected if you pass this within the context of a function scope or provide a valid global object like db:

Object.entries(db)
Object.getOwnPropertyDescriptors(db)

Is there a particular problem you are trying to solve in the mongo shell with your approach or are you just curious about the difference in behaviours? The mongo shell provides a more limited environment than a web browser or Node runtime.

Regards,
Stennie

I guess you can do that in a browser console. In the shell the following prints the this object:

var entries = Object.entries(this)
print(entries)