MongoDB如何使用$ date运算符查询? [英] MongoDB How to Query with the $date operator?
问题描述
编辑-上下文:我正在使用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运算符查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!