Datatypes Declaration in MongoDB for dbPointer, Symbol

Hi Team,

How to use below Data Type while creating collections.

dbPointer
javascript
symbol
javascriptWithScope
minKey
maxKey

Assist me with proper syntax and place of use.

Regards,
Jayaprakash

Hi @Jayaprakash_08777,

You may find below documents useful:

https://docs.mongodb.com/manual/reference/bson-types/

https://docs.mongodb.com/manual/core/schema-validation/

Also, if this doesn’t help, please elaborate more on your scenario.

Kanika

To expand a little:

  1. dbPointer - Deprecated as per 1

  2. javascript -

    // Create test records
    use test
    db.collection.insertMany([
       {a: 1},
       {a: 2},
       {a: 3}
    ])
    
    // Insert JS code
    db.system.js.save(
       {
          _id: "fnAdd",
          value: function (x){ return ++x; },
          description: "Increments x by 1"
       }
    )
    

    Result:

  3. symbol - Deprecated as per 1

  4. javascriptWithScope -
    Only possible via a driver or Compass and not Mongo shell. The scope argument is what makes it a javascriptWithScope type.
    image

    // The inserted doc will will look this
    {
       "_id": "fnWithScope",
       "value" : {
          "code" : "function (x){ return x * x; }",
          "scope" : {}
       }
    }
    

    NB: Prefixing the "code" and "scope" fields with $ will enable you use the above doc in Compass to create a javascriptWithScope type, however, any attempts to do this in the Mongo shell is futile as it will result in value being an object type.

    Result:

  5. minKey - Internal use only as per 1 but you can also reference doc 2

  6. maxKey - Internal use only as per 1 but you can also reference doc 2

Both javascript types are poorly documented, moreso the with-scope type (there’s hardly any documentation for this across multiple drivers and the Mongo shell).

References:
1 the first link Kanika posted
2 MongoDB Extended JSON (v2)
Store JS function in server
$where documentation

1 Like