在两个字符串之间过滤数据
Filter Data between two String
我有一个文档的创建日期属性,其格式如下:字符串类型的“05/03/2020”。
我必须从我的系统中提取创建日期早于“05/03/2020”的所有文档。
我试过:
db.MyCollection.find ({DateC: {$lte: "05/03/2020"}})
但它仍然是 returns 0.
有没有办法比较日期格式字符串是否在另一个日期之前?
谢谢
您可以使用 $dateFromString 运算符将 $DateC
转换为 ISODate,
- 确保您的输入应为 ISODate 格式,
db.MyCollection.find([
{
$expr: {
$lte: [
{
$dateFromString: {
dateString: "$DateC",
format: "%d/%m/%Y"
}
},
ISODate("2020-03-05T00:00:00.000Z")
]
}
}
])
I am not sure with your date format, I have predicted as d/m/y
, you can manage it your self if its different.
我有一个文档的创建日期属性,其格式如下:字符串类型的“05/03/2020”。
我必须从我的系统中提取创建日期早于“05/03/2020”的所有文档。
我试过:
db.MyCollection.find ({DateC: {$lte: "05/03/2020"}})
但它仍然是 returns 0.
有没有办法比较日期格式字符串是否在另一个日期之前?
谢谢
您可以使用 $dateFromString 运算符将 $DateC
转换为 ISODate,
- 确保您的输入应为 ISODate 格式,
db.MyCollection.find([
{
$expr: {
$lte: [
{
$dateFromString: {
dateString: "$DateC",
format: "%d/%m/%Y"
}
},
ISODate("2020-03-05T00:00:00.000Z")
]
}
}
])
I am not sure with your date format, I have predicted as
d/m/y
, you can manage it your self if its different.