I'm using a query console to make MongoDB queries.
let ids = db.collection.aggregate([
{
$match: { ... }
},
{
$group: {
_id: "$field"
}
}
]).map(doc => doc._id);
and then I want to use the variable in the 2nd query
db.collection.aggregate([
{
$match: {
"field": { $in: ids },
...
}
}
]);
note: field
is of a string type.
The 1st query is completed successfully, but the 2nd query fails with a strange error:
Error unmarshaling return nested exception is: WriteAbortedException writing aborted; NotSerializableException: com.oracle.truffle.api.TruffleStackTraceElement
I'm sure that something is wrong with the variable ids
, cause if I amend the 2nd query with "field": { $in: ['1', '2'] }
, then it works fine.
As it turned out, the key is to append .toArray()
, so the ids
variable actually becomes an array.
This works in DataGrip:
let ids = db.collection.aggregate([
{
$match: { ... }
},
{
$group: {
_id: "$field"
}
}
]).map(doc => doc._id).toArray();