MongoDB: $function 运算符不支持箭头函数
MongoDB: $function operator does not support arrow function
我在 testCollection
中有以下文件:
[
{
"_id": ObjectId("60562f98d171d52ef0a5bb27"),
"popularDate": ISODate("1947-08-15T00:00:00.000+05:30")
},
{
"_id": ObjectId("60562f98d171d52ef0a5bb28"),
"popularDate": ISODate("1950-01-26T00:00:00.000+05:30")
},
{
"_id": ObjectId("60562f98d171d52ef0a5bb29"),
"popularDate": ISODate("1994-01-15T00:00:00.000+05:30")
}
]
我正在使用正则表达式通过 $function
运算符过滤文档。我在使用 Query 1.
时得到了正确的输出
查询 1:
let yearRegex = /^1947/;
db.testCollection.find({
$expr: {
$function: {
body: function(popularDates, yearRegex) {
return yearRegex.test(popularDates)
},
args: [{ $toString: "$popularDates" }, yearRegex],
lang: "js"
}
}
});
查询 1 的输出:
{
"_id" : ObjectId("60562f98d171d52ef0a5bb27"),
"popularDate" : ISODate("1947-08-15T00:00:00.000+05:30")
}
但对于 查询 2,我正在获取所有文档,但过滤器无法正常工作。在 查询 2 中,我将函数体更改为箭头函数。
查询 2:
let yearRegex = /^1947/;
db.testCollection.find({
$expr: {
$function: {
body: (popularDate, yearRegex) => yearRegex.test(popularDate),
args: [{ $toString: "$popularDate" }, yearRegex],
lang: "js"
}
}
});
查询 2 的输出:
{
"_id": ObjectId("60562f98d171d52ef0a5bb27"),
"popularDate": ISODate("1947-08-15T00:00:00.000+05:30")
},
{
"_id": ObjectId("60562f98d171d52ef0a5bb28"),
"popularDate": ISODate("1950-01-26T00:00:00.000+05:30")
},
{
"_id": ObjectId("60562f98d171d52ef0a5bb29"),
"popularDate": ISODate("1994-01-15T00:00:00.000+05:30")
}
所以现在我的问题是为什么箭头函数在 $function
运算符中不起作用,或者我错过了什么。
MongoDB依赖于调用传递函数时使用javascriptcall
将this
设置为当前文档
箭头函数没有绑定到 this 或 super(参见 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions),因此它们在 MongoDB.[=13] 中的服务器端 javascript 中无法正常工作=]
我在 testCollection
中有以下文件:
[
{
"_id": ObjectId("60562f98d171d52ef0a5bb27"),
"popularDate": ISODate("1947-08-15T00:00:00.000+05:30")
},
{
"_id": ObjectId("60562f98d171d52ef0a5bb28"),
"popularDate": ISODate("1950-01-26T00:00:00.000+05:30")
},
{
"_id": ObjectId("60562f98d171d52ef0a5bb29"),
"popularDate": ISODate("1994-01-15T00:00:00.000+05:30")
}
]
我正在使用正则表达式通过 $function
运算符过滤文档。我在使用 Query 1.
查询 1:
let yearRegex = /^1947/;
db.testCollection.find({
$expr: {
$function: {
body: function(popularDates, yearRegex) {
return yearRegex.test(popularDates)
},
args: [{ $toString: "$popularDates" }, yearRegex],
lang: "js"
}
}
});
查询 1 的输出:
{
"_id" : ObjectId("60562f98d171d52ef0a5bb27"),
"popularDate" : ISODate("1947-08-15T00:00:00.000+05:30")
}
但对于 查询 2,我正在获取所有文档,但过滤器无法正常工作。在 查询 2 中,我将函数体更改为箭头函数。
查询 2:
let yearRegex = /^1947/;
db.testCollection.find({
$expr: {
$function: {
body: (popularDate, yearRegex) => yearRegex.test(popularDate),
args: [{ $toString: "$popularDate" }, yearRegex],
lang: "js"
}
}
});
查询 2 的输出:
{
"_id": ObjectId("60562f98d171d52ef0a5bb27"),
"popularDate": ISODate("1947-08-15T00:00:00.000+05:30")
},
{
"_id": ObjectId("60562f98d171d52ef0a5bb28"),
"popularDate": ISODate("1950-01-26T00:00:00.000+05:30")
},
{
"_id": ObjectId("60562f98d171d52ef0a5bb29"),
"popularDate": ISODate("1994-01-15T00:00:00.000+05:30")
}
所以现在我的问题是为什么箭头函数在 $function
运算符中不起作用,或者我错过了什么。
MongoDB依赖于调用传递函数时使用javascriptcall
将this
设置为当前文档
箭头函数没有绑定到 this 或 super(参见 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions),因此它们在 MongoDB.[=13] 中的服务器端 javascript 中无法正常工作=]