Nếu bạn có tài liệu lưu trữ ngày tháng dưới dạng đối tượng Date, nhưng bạn muốn trả về chúng ở định dạng khác, bạn có thể sử dụng toán tử đường dẫn tổng hợp
{ "_id" : 1, "name" : "Scratch", "born" : ISODate["2021-01-03T23:30:15.123Z"] } { "_id" : 2, "name" : "Meow", "born" : ISODate["2019-12-08T04:00:20.112Z"] } { "_id" : 3, "name" : "Fluffy", "born" : ISODate["2020-09-24T10:45:01.007Z"] }2
Ví dụ: bạn có thể muốn một ngày được trả về ở định dạng
db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
0 thay vì định dạng dài db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
1 bao gồm phút, giây, mili giây, v.v.Toán tử
{ "_id" : 1, "name" : "Scratch", "born" : ISODate["2021-01-03T23:30:15.123Z"] } { "_id" : 2, "name" : "Meow", "born" : ISODate["2019-12-08T04:00:20.112Z"] } { "_id" : 3, "name" : "Fluffy", "born" : ISODate["2020-09-24T10:45:01.007Z"] }2 chuyển đổi đối tượng Date thành một chuỗi và tùy chọn cho phép bạn chỉ định định dạng cho kết quả đầu ra
Thí dụ
Giả sử chúng ta có một bộ sưu tập tên là
db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
3 với các tài liệu sau{ "_id" : 1, "name" : "Scratch", "born" : ISODate["2021-01-03T23:30:15.123Z"] } { "_id" : 2, "name" : "Meow", "born" : ISODate["2019-12-08T04:00:20.112Z"] } { "_id" : 3, "name" : "Fluffy", "born" : ISODate["2020-09-24T10:45:01.007Z"] }
Chúng tôi có thể sử dụng
{ "_id" : 1, "name" : "Scratch", "born" : ISODate["2021-01-03T23:30:15.123Z"] } { "_id" : 2, "name" : "Meow", "born" : ISODate["2019-12-08T04:00:20.112Z"] } { "_id" : 3, "name" : "Fluffy", "born" : ISODate["2020-09-24T10:45:01.007Z"] }2 để trả lại tài liệu đó với ngày ở định dạng khác
Ví dụ: hãy xóa giây và mili giây khỏi ngày
db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
Kết quả
{ "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" } { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" } { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
Chúng tôi có thể định dạng ngày bằng cách sử dụng tham số
db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
5. Đây là một tham số tùy chọn cho phép bạn sử dụng các chỉ định định dạng bằng 0 hoặc nhiều hơn để cho biết cách định dạng ngày thángXem MongoDB
{ "_id" : 1, "name" : "Scratch", "born" : ISODate["2021-01-03T23:30:15.123Z"] } { "_id" : 2, "name" : "Meow", "born" : ISODate["2019-12-08T04:00:20.112Z"] } { "_id" : 3, "name" : "Fluffy", "born" : ISODate["2020-09-24T10:45:01.007Z"] }2 Format Specifiers để biết danh sách đầy đủ các định dạng định dạng có thể được sử dụng với toán tử
{ "_id" : 1, "name" : "Scratch", "born" : ISODate["2021-01-03T23:30:15.123Z"] } { "_id" : 2, "name" : "Meow", "born" : ISODate["2019-12-08T04:00:20.112Z"] } { "_id" : 3, "name" : "Fluffy", "born" : ISODate["2020-09-24T10:45:01.007Z"] }2
Ngày ở định dạng db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
8
db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
Đây là một ví dụ khác chuyển đổi ngày thành định dạng
db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
8db.cats.aggregate[
[
{
$project: {
_id: 0,
formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } }
}
}
]
]
Kết quả
db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
0Ngày ở định dạng db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
0
db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
Hoặc để đặt nó ở định dạng
db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
0, chúng ta chỉ cần chuyển đổi hai định dạng định dạng đầu tiên xung quanhdb.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
3Kết quả
db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
4Trả lại một phần ngày duy nhất
Bạn có thể bao gồm nhiều hoặc ít thông số định dạng tùy ý. Ví dụ: bạn có thể chỉ sử dụng một công cụ xác định định dạng để chỉ xuất phần năm của ngày
Thí dụ
db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
5Kết quả
db.cats.aggregate[
[
{
$project: {
name: 1,
formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
}
}
]
]
6Mặc dù vậy, hãy nhớ rằng có nhiều cách khác để trích xuất chỉ một phần ngày từ một đối tượng Date. Ví dụ: bạn có thể sử dụng toán tử
{ "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" } { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" } { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }2 để trích xuất năm
Dưới đây là các toán tử khác nhau để trích xuất từng phần ngày cụ thể
{ "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" } { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" } { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
3{ "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" } { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" } { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
4{ "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" } { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" } { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
5{ "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" } { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" } { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
6{ "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" } { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" } { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
7{ "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" } { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" } { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
8{ "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" } { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" } { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
9
0db.cats.aggregate[ [ { $project: { _id: 0, formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } } } } ] ]
1db.cats.aggregate[ [ { $project: { _id: 0, formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } } } } ] ]
2db.cats.aggregate[ [ { $project: { _id: 0, formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } } } } ] ]
3db.cats.aggregate[ [ { $project: { _id: 0, formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } } } } ] ]
4db.cats.aggregate[ [ { $project: { _id: 0, formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } } } } ] ]
5db.cats.aggregate[ [ { $project: { _id: 0, formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } } } } ] ]
Bạn cũng có thể sử dụng toán tử
db.cats.aggregate[
[
{
$project: {
_id: 0,
formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } }
}
}
]
]
6 để trả về một tài liệu chứa tất cả các phần ngày khác nhau được phân tách thành trường riêng của chúng