How do I convert the field date of type String
to Date
before doing a filter to get all dates greater than the current date? Thanks!
Asked
Active
Viewed 3,109 times
3

Yong Shun
- 35,286
- 4
- 24
- 46

SarahAlexa
- 43
- 1
- 4
-
Give some test data and expected result would be better – YuTing Oct 31 '21 at 04:03
2 Answers
1
data
[
{
_id: 1,
item: "apple",
qty: 5,
order_date: new Date("2018-03-10")
},
{
_id: 2,
item: "pie",
qty: 10,
order_date: new Date("2018-03-12")
},
{
_id: 3,
item: "ice cream",
qty: 2,
price: "4.99",
order_date: "2018-03-05"
},
{
_id: 4,
item: "almonds",
qty: 5,
price: 5,
order_date: "2018-03-05 +10:00"
}
]
$toDate
db.collection.aggregate([
{
$addFields: {
convertedDate: {
$toDate: "$order_date"
}
}
}
])

YuTing
- 6,555
- 2
- 6
- 16