如何使用MongoDB筛选特定日期格式的查询?
若要过滤特定日期格式的查询,请使用$dateToString。让我们创建一个包含文档的集合-
> db.demo433.insertOne({"DueDate":new Date("2019-11-23")});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e771278bbc41e36cc3cae91")
}
> db.demo433.insertOne({"DueDate":new Date("2020-01-03")});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e771290bbc41e36cc3cae92")
}在find()方法的帮助下显示集合中的所有文档-
> db.demo433.find();
这将产生以下输出-
{ "_id" : ObjectId("5e771278bbc41e36cc3cae91"), "DueDate" : ISODate("2019-11-23T00:00:00Z") }
{ "_id" : ObjectId("5e771290bbc41e36cc3cae92"), "DueDate" : ISODate("2020-01-03T00:00:00Z") }以下是对MongoDB中特定日期格式的查询进行过滤的查询-
> db.demo433.aggregate([
... { $addFields: {stringDate: { $dateToString: { format: "%Y-%m-%d", date: "$DueDate" } } } },
... { $match: {"stringDate":"2020-01-03"}},
... { $project:{"stringDate":0}}
... ])这将产生以下输出-
{ "_id" : ObjectId("5e771290bbc41e36cc3cae92"), "DueDate" : ISODate("2020-01-03T00:00:00Z") }热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短