|
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 ] }
|
|