Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-50614

JS 'Set' type does not serialize well to BSON

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Backlog
    • Component/s: JavaScript
    • Labels:

      Description

      As you can see below - it just happens to return an empty object, but proceeds without error. It seems better to either (a) error or (b) return an array. We don't have a 'set' data type in the BSON spec, so I don't see how we could get it to work and round trip seamlessly, but this experience isn't great.

      > db.foo.aggregate([{
          $project: {
              js_res: {
                  $function: {
                      lang: "js",
                      body: function() {
                          return new Set([1, 2]);
                      },
                      args: []
                  }
              }
          }
      }])
      { "_id" : ObjectId("5f3c24f10efd1cb7f0a88f4a"), "js_res" : {  } }
      { "_id" : ObjectId("5f3c24f60efd1cb7f0a88f4b"), "js_res" : {  } }
      { "_id" : ObjectId("5f47f9411945d1527c6b0368"), "js_res" : {  } }
      { "_id" : ObjectId("5f47f9c71945d1527c6b0369"), "js_res" : {  } }
      { "_id" : ObjectId("5f47fa721945d1527c6b036a"), "js_res" : {  } }
      

      As a workaround, you can use the [...set] syntax to expand the set into an array:

      > db.foo.aggregate([{
          $project: {
              js_res: {
                  $function: {
                      lang: "js",
                      body: function() {
                          return [...new Set([1, 2])];
                      },
                      args: []
                  }
              }
          }
      }])
      { "_id" : ObjectId("5f3c24f10efd1cb7f0a88f4a"), "js_res" : [ 1, 2 ] }
      { "_id" : ObjectId("5f3c24f60efd1cb7f0a88f4b"), "js_res" : [ 1, 2 ] }
      { "_id" : ObjectId("5f47f9411945d1527c6b0368"), "js_res" : [ 1, 2 ] }
      { "_id" : ObjectId("5f47f9c71945d1527c6b0369"), "js_res" : [ 1, 2 ] }
      { "_id" : ObjectId("5f47fa721945d1527c6b036a"), "js_res" : [ 1, 2 ] }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              backlog-query-optimization Backlog - Query Optimization
              Reporter:
              charlie.swanson Charlie Swanson
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated: