Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Java中的MongoDB$where子句中使用lambda函数_Javascript_Java_Mongodb_Lambda - Fatal编程技术网

Javascript 在Java中的MongoDB$where子句中使用lambda函数

Javascript 在Java中的MongoDB$where子句中使用lambda函数,javascript,java,mongodb,lambda,Javascript,Java,Mongodb,Lambda,我试图使用$where子句在Java中执行JavaScript等价查询。 JavaScript查询如下所示: var currDate = new ISODate() db.getCollection('rides') .find( { "startDate" : { "$lte" : currDate }, "stopDate" : { "$gte" : currDate },

我试图使用$where子句在Java中执行JavaScript等价查询。 JavaScript查询如下所示:

var currDate = new ISODate()
db.getCollection('rides')
.find(
{
        "startDate" : {
            "$lte" : currDate
        },
        "stopDate" : {
            "$gte" : currDate
         },
        $where : function() { return (this.weekday == new Date(new ISODate().getTime() + this.timeOffset).getDay()) }
}
)
工作日时间偏移是文档字段。 此查询在MongoDB shell中运行良好。我试图找到一种用Java8编写此查询的方法。 我尝试了以下方法:

BasicDBObject query =  new BasicDBObject("stopDate",new BasicDBObject("$gte", currDate))
                .append("startDate",new BasicDBObject("$lte", currDate))
                .append("weekday",
                        new BasicDBObject("$where", () ->
                        {
                            return (this.weekday == currDate + this.timeOffset)
                        }));
然而,我甚至不能编译这段代码。Java无法识别此。 有没有办法用Java完成查询

提前感谢您的帮助

好的

我已经为同一个查询找到了另一个解决方案。由于以下Javascript查询相当于发布的查询:

var currDate = new ISODate()
db.getCollection('rides')
.find(
{
    "startDate" : {
        "$lte" : currDate
    },
    "stopDate" : {
        "$gte" : currDate
     },
    $where : 'this.weekday == new Date(new Date().getTime() + this.timeOffset).getDay()'
})
我可以用Java编写同样的代码:

BasicDBObject query =  new BasicDBObject("stopDate",new BasicDBObject("$gte", currDate))
            .append("startDate", new BasicDBObject("$lte", currDate))
            .append("$where", "this.weekday == new Date(new Date().getTime() + this.timeOffset).getDay()");
这对我来说非常有效

Document whereDoc=新文档(
Document whereDoc = new Document(
    "$where",
    "function(){var j=false; [2,4].forEach(i=>{if([4,5].indexOf(i)>-1){j=true;}}); return j;}"
);

Query query = new BasicQuery(whereDoc);
query.addCriteria(Criteria.where("ent_id").is("google"));
query.addCriteria(Criteria.where("app_id").is("pic"));
List<Map> result = mongoTemplate.find(query, Map.class, "googletest");
“$where”, “函数(){var j=false;[2,4].forEach(i=>{if([4,5].indexOf(i)>-1){j=true;}});返回j;}” ); 查询=新基本查询(whereDoc); query.addCriteria(Criteria.where(“entu id”)是(“谷歌”); query.addCriteria(Criteria.where(“app_id”)为(“pic”); List result=mongoTemplate.find(查询,Map.class,“googletest”);
如果你能解释你做了什么以及为什么做,而不只是贴一堵代码墙,答案通常会更有帮助。此外,这看起来与询问的查询不太一样(没有日期和其他字段被查询)。