MongoDB如何使用$ date运算符查询?

编程入门 行业动态 更新时间:2024-10-10 13:19:42
本文介绍了MongoDB如何使用$ date运算符查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

编辑-上下文:我正在使用Talend ETL工具,并在查询中使用ISODate或Date或new Date,如下所示失败并出现错误,因此我需要解决:

{'dt' : ISODate('2014-01-01') } {'dt' : Date('2014-01-01') } {'dt' : new Date('2014-01-01') }

如果没有以下错误,我将无法执行此操作:

at com.mongodb.util.JSONParser.read(JSON.java:272) at com.mongodb.util.JSONParser.parse(JSON.java:161) at com.mongodb.util.JSONParser.parseObject(JSON.java:231) at com.mongodb.util.JSONParser.parse(JSON.java:195) at com.mongodb.util.JSONParser.parse(JSON.java:145) at com.mongodb.util.JSON.parse(JSON.java:81) at com.mongodb.util.JSON.parse(JSON.java:66)

大概是因为ETL工具调用:

com.mongodb.DBObject myQuery_tMongoDBInput_1 = (com.mongodb.DBObject) com.mongodb.util.JSON .parse("{'dt': new Date('2000-01-01T08:00:00Z')}");

鉴于我不能在com.mongodb.util.JSON.parse()方法的查询中使用new Date(),是否可以解决?

我正在使用MongoDB v2.6.3,无法使$ date运算符正常工作.

db.testdate.insert( {dt:ISODate('2014-01-01')} ) db.testdate.find()

db.testdate.find( {dt : {$date : '2014-01-01T00:00:00Z'}} )

错误:{ "$ err":无法规范化查询:BadValue未知顶级运算符:$ date", 代码":17287}

db.testdate.find( {dt : {$gte : {$date : '2000-01-01T00:00:00Z'}}} )

我还看到了其他示例,这些示例在使用$ date运算符的情况下有效,但无法在我的计算机上实现.

有人知道为什么吗?

解决方案

$date是为工具mongoimport,mongoexport等准备的.Mongo shell无法识别它,您应该使用Date()或ISODate()代替.

Edit - Context: I'm using the Talend ETL tool and using ISODate or Date or new Date in the query like the following fail with an error, so I need a work around:

{'dt' : ISODate('2014-01-01') } {'dt' : Date('2014-01-01') } {'dt' : new Date('2014-01-01') }

I cannot do so without the following error:

at com.mongodb.util.JSONParser.read(JSON.java:272) at com.mongodb.util.JSONParser.parse(JSON.java:161) at com.mongodb.util.JSONParser.parseObject(JSON.java:231) at com.mongodb.util.JSONParser.parse(JSON.java:195) at com.mongodb.util.JSONParser.parse(JSON.java:145) at com.mongodb.util.JSON.parse(JSON.java:81) at com.mongodb.util.JSON.parse(JSON.java:66)

presumably because the ETL tool calls:

com.mongodb.DBObject myQuery_tMongoDBInput_1 = (com.mongodb.DBObject) com.mongodb.util.JSON .parse("{'dt': new Date('2000-01-01T08:00:00Z')}");

Given that I cannot use the new Date() in the query for the com.mongodb.util.JSON.parse() method, is there a work around?

I'm using MongoDB v2.6.3 and cannot get the $date operator to work.

db.testdate.insert( {dt:ISODate('2014-01-01')} ) db.testdate.find()

db.testdate.find( {dt : {$date : '2014-01-01T00:00:00Z'}} )

error: { "$err" : "Can't canonicalize query: BadValue unknown top level operator: $date", "code" : 17287 }

db.testdate.find( {dt : {$gte : {$date : '2000-01-01T00:00:00Z'}}} )

I've seen other examples where the use of the $date operator worked, but cannot get it to do so on my machine.

Does anyone know why?

解决方案

$date is prepared for tools mongoimport, mongoexport, etc. Mongo shell can't recognize it, you should use Date() or ISODate() instead.

更多推荐

MongoDB如何使用$ date运算符查询?

本文发布于:2023-10-30 04:45:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541807.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   运算符   MongoDB   date

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!