Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-980

Mixing behavior when executing server related method

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 1.0.5
    • Component/s: AsyncWriter
    • 5

      Problem Statement/Rationale

      MongoSh have mixing behavior when executing code directly and executing string code by using eval or Function.

      Steps to Reproduce

      Executing the following snippet to reproduce:

      1. Create testing object

       

      // Raw code
      Test = function () { a =  dbs.admin.runCommand({rolesInfo: `root`}); console.log(a.constructor.name); return a; }
      
      // Using eval
      eval('global.Test2 = function () { a =  dbs.admin.runCommand({rolesInfo: `root`}); console.log(a.constructor.name); return a; }')
      
      // Using Function
      Function('global.Test3 = function () { a =  dbs.admin.runCommand({rolesInfo: 
      `root`}); console.log(a.constructor.name); return a; }')()

      2. Exectuting each function

       

       

      > Test()
      
      > Test2()
      
      > Test3() 
      

       

      Expected Results

      All 3 test printing Object and roles result. Same behavior since code are the same.

       

      > Test()
      Object
      {
        roles: [
          {
            db: 'admin',
            role: 'root',
            roles: [],
            inheritedRoles: [],
            isBuiltin: true
          }
        ],
        ok: 1,
        '$clusterTime': {
          clusterTime: Timestamp({ t: 1631014883, i: 1 }),
          signature: {
            hash: Binary(Buffer.from("eabb594687cc5dd2eab350cd540036c000b25912", "hex"), 0),
            keyId: Long("6995103262357585924")
          }
        },
        operationTime: Timestamp({ t: 1631014883, i: 1 })
      }

      Actual Results

       

      Test2() and Test3() returning Promise instead of Object

      > Test2()
      Promise
      {
        roles: [
          {
            db: 'admin',
            role: 'root',
            roles: [],
            inheritedRoles: [],
            isBuiltin: true
          }
        ],
        ok: 1,
        '$clusterTime': {
          clusterTime: Timestamp({ t: 1631014883, i: 1 }),
          signature: {
            hash: Binary(Buffer.from("eabb594687cc5dd2eab350cd540036c000b25912", "hex"), 0),
            keyId: Long("6995103262357585924")
          }
        },
        operationTime: Timestamp({ t: 1631014883, i: 1 })
      }
      
      > Test3()
      Promise
      {
        roles: [
          {
            db: 'admin',
            role: 'root',
            roles: [],
            inheritedRoles: [],
            isBuiltin: true
          }
        ],
        ok: 1,
        '$clusterTime': {
          clusterTime: Timestamp({ t: 1631014883, i: 1 }),
          signature: {
            hash: Binary(Buffer.from("eabb594687cc5dd2eab350cd540036c000b25912", "hex"), 0),
            keyId: Long("6995103262357585924")
          }
        },
        operationTime: Timestamp({ t: 1631014883, i: 1 })
      }

      Additional Notes

      Because the Promise is returned, code saved on server cannot run as expected in MongoSh.

            Assignee:
            Unassigned Unassigned
            Reporter:
            bdtri.cse.hcmut@gmail.com Billy Bùi
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: