Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -
> db.demo178.insertOne({"DueDate":new ISODate("2019-01-10T06:18:20.474Z")});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e397bd89e4f06af551997f5")
}
> db.demo178.insertOne({"DueDate":new ISODate("2020-11-10T18:05:11.474Z")});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e397bf39e4f06af551997f6")
}
> db.demo178.insertOne({"DueDate":new ISODate("2020-03-15T07:05:10.474Z")});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e397c039e4f06af551997f7")
}
> db.demo178.insertOne({"DueDate":new ISODate("2020-06-11T16:05:10.474Z")});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e397c0f9e4f06af551997f8")
} Hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find () -
> db.demo178.find();
Điều này sẽ tạo ra kết quả sau -
{ "_id" : ObjectId("5e397bd89e4f06af551997f5"), "DueDate" : ISODate("2019-01-10T06:18:20.474Z") }
{ "_id" : ObjectId("5e397bf39e4f06af551997f6"), "DueDate" : ISODate("2020-11-10T18:05:11.474Z") }
{ "_id" : ObjectId("5e397c039e4f06af551997f7"), "DueDate" : ISODate("2020-03-15T07:05:10.474Z") }
{ "_id" : ObjectId("5e397c0f9e4f06af551997f8"), "DueDate" : ISODate("2020-06-11T16:05:10.474Z") } Sau đây là truy vấn để tìm nạp các bản ghi ngày trong một phạm vi -
> db.demo178.aggregate([
...{
... "$redact": {
... "$cond": {
... "if": {
... "$and": [
... { "$gt": [ {"$hour": "$DueDate"}, 5] },
... { "$lt": [ {"$hour": "$DueDate"}, 9] }
... ]
... },
... "then": "$$KEEP",
... "else": "$$PRUNE"
... }
... }
... }
...]) Điều này sẽ tạo ra kết quả sau -
{ "_id" : ObjectId("5e397bd89e4f06af551997f5"), "DueDate" : ISODate("2019-01-10T06:18:20.474Z") }
{ "_id" : ObjectId("5e397c039e4f06af551997f7"), "DueDate" : ISODate("2020-03-15T07:05:10.474Z") }