Reputation: 59
I have a problem how to get data between start date because it's just string and my document schema like this
"_id" : ObjectId("5ab92bdd6fbcd28fb0c68ff6"),
"username" : "jumbreng",
"startdate" : "26-03-2018",
"enddate" : "11-08-2018",
"status" : 1
i have been try create aggregation like this
db.presensi.aggregate( [ {
$project: {
"username": "$username",
"startdate": {
$dateFromString: {
dateString: '$startdate',
}
},
"enddate" :{
$dateFromString: {
dateString: '$enddate',
}
}
}
} ] )
but i don't know hot to make this have range like find($lte and $gte) should i update this aggregation to my collection?and then i can crate statement find($lte and $gte)? how to make aggregation to my collection?
Upvotes: 4
Views: 2339
Reputation: 144
$dateFromString aggregation operator can help you.
try it:
db.user.aggregate(
// Pipeline
[
// Stage 1
{
$project: {
_id: true,
startdate: true,
enddate: true,
status: true,
username: "$username",
isBetween: {
$and: [{
$lt: [{
$dateFromString: {
dateString: '$startdate',
}
}, {
$dateFromString: {
dateString: '27-03-2018',
}
}]
}, {
$gt: [{
$dateFromString: {
dateString: '$startdate',
}
}, {
$dateFromString: {
dateString: '25-03-2018',
}
}]
}]
}
}
},
// Stage 2
{
$match: {
"isBetween": true
}
},
], {
cursor: {
batchSize: 50
}
});
Upvotes: 5