mongodb에서 ISODate를 사용한 날짜 쿼리가 작동하지 않는 것 같습니다.
MongoDB에서 작동하는 가장 기본적인 날짜 쿼리조차 얻을 수없는 것 같습니다. 다음과 같은 문서가 있습니다.
{
"_id" : "foobar/201310",
"ap" : "foobar",
"dt" : ISODate("2013-10-01T00:00:00.000Z"),
"tl" : 375439
}
그리고 다음과 같은 쿼리 :
{
"dt" : {
"$gte" : {
"$date" : "2013-10-01T00:00:00.000Z"
}
}
}
실행 결과 가 0입니다 .
db.mycollection.find({
"dt" : { "$gte" : { "$date" : "2013-10-01T00:00:00.000Z"}}
})
왜 이것이 작동하지 않는지 아십니까?
참고 로이 쿼리는 Spring의 MongoTemplate에 의해 생성 되므로 궁극적으로 MongoDB에 전송되는 쿼리를 직접 제어 할 수 없습니다.
(추신)
> db.version()
2.4.7
감사!
MongoDB Extended JSON$date
의 일부 이지만 이것이 기본값으로 얻는 것입니다. 실제로 쿼리의 일부로 사용할 수 있다고 생각하지 않습니다.mongoexport
$date
아래와 같이 정확한 검색을 시도하면 :
db.foo.find({dt: {"$date": "2012-01-01T15:00:00.000Z"}})
오류가 발생합니다.
error: { "$err" : "invalid operator: $date", "code" : 10068 }
이 시도:
db.mycollection.find({
"dt" : {"$gte": new Date("2013-10-01T00:00:00.000Z")}
})
또는 ( @ user3805045의 의견 다음 ) :
db.mycollection.find({
"dt" : {"$gte": ISODate("2013-10-01T00:00:00.000Z")}
})
ISODate
시간없이 날짜를 비교해야 할 수도 있습니다 ( @MattMolnar로 표시 ).
mongo Shell의 데이터 유형에 따르면 둘 다 동일해야합니다.
mongo 쉘은 날짜를 문자열 또는 Date 객체로 반환하는 다양한 방법을 제공합니다.
- 현재 날짜를 문자열로 반환하는 Date () 메서드
- ISODate () 래퍼를 사용하여 Date 객체를 반환하는 new Date () 생성자
- ISODate () 래퍼를 사용하여 Date 객체를 반환하는 ISODate () 생성자
사용 ISODate
하면 여전히 Date 객체가 반환되어야합니다 .
{"$date": "ISO-8601 string"}
엄격한 JSON 표현이 필요할 때 사용할 수 있습니다. 한 가지 가능한 예는 Hadoop 커넥터입니다.
로부터 MongoDB의 요리 책 페이지 댓글 :
"dt" :
{
"$gte" : ISODate("2014-07-02T00:00:00Z"),
"$lt" : ISODate("2014-07-03T00:00:00Z")
}
This worked for me. In full context, the following command gets every record where the dt
date field has a date on 2013-10-01 (YYYY-MM-DD) Zulu:
db.mycollection.find({ "dt" : { "$gte" : ISODate("2013-10-01T00:00:00Z"), "$lt" : ISODate("2013-10-02T00:00:00Z") }})
Try this:
{ "dt" : { "$gte" : ISODate("2013-10-01") } }
I am using robomongo as the mongodb client gui and the below worked for me
db.collectionName.find({"columnWithDateTime" : {
$lt:new ISODate("2016-02-28T00:00:00.000Z")}})
On the app side I am using nodejs based driver mongodb(v1.4.3),the application uses datepicker in the ui which gives date like YYYY-mm-dd, this is then appended with default time like 00:00:00 and then given to the new Date()
constructor and then supplied to the mongodb criteria object,I think the driver converts the date to ISO date and the query then works and gives desired output, however the same new Date()
constructor does not work or show same output on robo mongo,for the same criteria,which is weird,since I used robomongo to cross check my criteria objects.
Whereas the default cli mongoshell works well with both ISODate
and new Date()
In json strict mode, you'll have to keep the order:
{
"dt": {
"$gte": {
"$date": "2013-10-01T00:00:00.000Z"
}
}
}
Only thing which worked to define my search queries on mlab.com.
In the MongoDB shell:
db.getCollection('sensorevents').find({from:{$gt: new ISODate('2015-08-30 16:50:24.481Z')}})
In my nodeJS code ( using Mongoose )
SensorEvent.Model.find( {
from: { $gt: new Date( SensorEventListener.lastSeenSensorFrom ) }
} )
I am querying my sensor events collection to return values where the 'from' field is greater than the given date
this is my document
"_id" : ObjectId("590173023c488e9a48e903d6"),
"updatedAt" : ISODate("2017-04-27T04:26:42.709Z"),
"createdAt" : ISODate("2017-04-27T04:26:42.709Z"),
"flightId" : "590170f97cb84116075e2680",
"_id" : ObjectId("590173023c488e9a48e903d6"),
"updatedAt" : ISODate("2017-04-28T03:26:42.609Z"),
"createdAt" : ISODate("2017-04-28T03:26:42.609Z"),
"flightId" : "590170f97cb84116075e2680",
now i want to find every 27th date document.so i used this....
> db.users.find({createdAt:{"$gte":ISODate("2017-04-27T00:00:00Z"),"$lt":ISODate("2017-04-28T00:00:00Z") }}).count()
result:1
this worked for me.
참고URL : https://stackoverflow.com/questions/19819870/date-query-with-isodate-in-mongodb-doesnt-seem-to-work
'IT' 카테고리의 다른 글
Visual Studio에서 다른 폴더로 프로젝트 이동 (0) | 2020.05.28 |
---|---|
Xcode에서 벡터 이미지는 어떻게 작동합니까 (예 : pdf 파일)? (0) | 2020.05.28 |
javax.faces.application.ViewExpiredException :보기를 복원 할 수 없습니다 (0) | 2020.05.28 |
Eclipse에서 프로젝트를 JAR로 자동 빌드 (0) | 2020.05.28 |
스레드 간 작업이 유효하지 않음 : 에서 작성된 스레드 이외의 스레드에서 액세스하는 제어 'textBox1' (0) | 2020.05.28 |