根据日期在MongoDB中返回查询?
为了基于MongoDB中的日期返回查询,让我们举个例子。
为了理解这个概念,让我们用文档创建一个集合。使用文档创建集合的查询如下-
> db.returnQueryFromDate.insertOne({"PassengerName":"John","PassengerAge":23,"PassengerArrivalTime":new ISODate("2018-03-10 14:45:56")});{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a57be9064dcd4a68b70e4")
}
> db.returnQueryFromDate.insertOne({"PassengerName":"Larry","PassengerAge":21,"PassengerArrivalTime":new ISODate("2018-05-19 11:10:23")});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e5")
}
> db.returnQueryFromDate.insertOne({"PassengerName":"Mike","PassengerAge":24,"PassengerArrivalTime":new ISODate("2018-08-25 16:40:12")});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e6")
}
>db.returnQueryFromDate.insertOne({"PassengerName":"Carol","PassengerAge":26,"PassengerArrivalTime":new ISODate("2019-01-29 09:45:10")});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e7")
}
在find()
method的帮助下显示集合中的所有文档。查询如下-
> db.returnQueryFromDate.find().pretty();
以下是输出-
{"_id" : ObjectId("5c8a57be9064dcd4a68b70e4"),
"PassengerName" : "John",
"PassengerAge" : 23,
"PassengerArrivalTime" : ISODate("2018-03-10T14:45:56Z")
}
{
"_id" : ObjectId("5c8a57bf9064dcd4a68b70e5"),
"PassengerName" : "Larry",
"PassengerAge" : 21,
"PassengerArrivalTime" : ISODate("2018-05-19T11:10:23Z")
}
{
"_id" : ObjectId("5c8a57bf9064dcd4a68b70e6"),
"PassengerName" : "Mike",
"PassengerAge" : 24,
"PassengerArrivalTime" : ISODate("2018-08-25T16:40:12Z")
}
{
"_id" : ObjectId("5c8a57bf9064dcd4a68b70e7"),
"PassengerName" : "Carol",
"PassengerAge" : 26,
"PassengerArrivalTime" : ISODate("2019-01-29T09:45:10Z")
}
这是基于日期的返回查询。日期大于2018-05-19T11:10:23Z的记录将显示-
> db.returnQueryFromDate.find({"PassengerArrivalTime" : { $gte : new ISODate("2018-05-19T11:10:23Z") }}).pretty();
以下是输出-
{"_id" : ObjectId("5c8a57bf9064dcd4a68b70e5"),
"PassengerName" : "Larry",
"PassengerAge" : 21,
"PassengerArrivalTime" : ISODate("2018-05-19T11:10:23Z")
}
{
"_id" : ObjectId("5c8a57bf9064dcd4a68b70e6"),
"PassengerName" : "Mike",
"PassengerAge" : 24,
"PassengerArrivalTime" : ISODate("2018-08-25T16:40:12Z")
}
{
"_id" : ObjectId("5c8a57bf9064dcd4a68b70e7"),
"PassengerName" : "Carol",
"PassengerAge" : 26,
"PassengerArrivalTime" : ISODate("2019-01-29T09:45:10Z")
}
以上是 根据日期在MongoDB中返回查询? 的全部内容, 来源链接: utcz.com/z/341115.html