iCurious :
I have Document Person
like
{
"_id": {
"$oid": "5e44659dcd"
},
"record": {
"Date_Of_Birth": "9/25/2018"
},
"_class": "com.example.entities.Birth"
}
Record
is of type object
, Date_Of_Birth
is of type String
but Date
has been stored in it and I want to execute query to use $gte and $lte operations on that. I am using spring data mongodb. It seems first I have to use $dateFromString to convert it to date, But this seems to be aggregation which I don't think I require.
Below is the code which I have created so far
Pageable pageable = PageRequest.of(page, size);
Query query = new Query(
Criteria
.where("record.Date_Of_Birth").gte(DateOperators.dateFromString(startDate).withFormat("mm/dd/yyyy"))
.andOperator(Criteria.where("record.Date_Of_Birth").lte(DateOperators.dateFromString(endDate).withFormat("mm/dd/yyyy"))
)).with(pageable);
which generates this query
{ "record.Date_Of_Birth" : { "$gte" : { "value" : { "dateString" : "9/25/2018", "format" : "mm/dd/yyyy"}}}, "$and" : [{ "record.Date_Of_Birth" : { "$lte" : { "value" : { "dateString" : "9/25/2018", "format" : "mm/dd/yyyy"}}}}]}
and is not working at all. Any idea?
Joe :
$expr permits using aggregation operators inside the query. This will permit you to convert the stored value to a date so you can do an apples to apples comparison.
db.collection.find({
$expr: {
$gte: [
{$dateFromString: {
dateString: "$record.Date_Of_Birth",
format: "%m/%d/%Y",
timeZone: "EST5EDT"}},
{$dateFromString: {
dateString: "9/25/2018",
format: "%m/%d/%Y",
timeZone: "EST5EDT"}}
]
}
})
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=417319&siteId=1