Cập nhật 2018-06-26 đã sửa mã để sử dụng measter [] thay vì ngày mới []
Tôi đã giải quyết điều này bằng cách sử dụng measterjs timezone [//momentjs.com/timezone/] để chuyển đổi ngày/giờ địa phương sang trường số chỉ ngày, sau đó tôi lưu trữ ngày làm số.
Trong mã JavaScript của tôi [bên ngoài MongoDB]:
var localDateOnly = function[timezone, d] {
if [d == undefined] { d = new Date[]; } // current date/time
return Number[ moment[d].tz[timezone].format["YYYYMMDD"] ];
}
Sau đó, tôi lưu trữ một trường chỉ dành cho ngày trong hồ sơ Mongo.
var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
Sau đó, trong mã JavaScript của tôi, tôi có thể dễ dàng truy vấn hôm nay, ngày mai, những ngày cụ thể, v.v.
// today
var myDate = localDateOnly["America/New_York"];
db.birthdays.find[ { dateonly: myDate } ];
// tomorrow
var myDate = localDateOnly[
"America/New_York",
moment[].add[ 1, "days" ]
]; // tomorrow
db.birthdays.find[ { dateonly: myDate } ];
// someone wants to know birthdays on the calendar on July 15, 2015,
// no timezone math needed, just type the date in YYYYMMDD format
db.birthdays.find[ { dateonly: 20150715 } ];
Hy vọng điều này sẽ giúp ai đó. Quyết định lưu trữ dưới dạng một con số để kiểm tra hiệu suất và hiệu lực. Trước khi chèn, tôi cũng kiểm tra một ngày hợp lệ bằng gói thời điểm:
moment[ 20150715, "YYYYMMDD", true ].isValid[] // returns true & allowed to store in the database
moment[ 150715, "YYYYMMDD", true ].isValid[] // returns false, don't insert into db
Mã sạch hơn khi lưu trữ các số nguyên đại diện cho ngày và giúp Mongo dễ dàng tìm thấy phạm vi. Giống như, tìm sinh nhật vào năm 2015 sẽ là
var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
0 hoặc var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
1. Tài liệu về nhà → Hướng dẫn sử dụng MongoDB → MongoDB Manual
var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
2 return một ngày dưới dạng chuỗi hoặc là đối tượng ngày.Returns a date either as a string or as a Date object.
2 Trả về ngày hiện tại dưới dạng chuỗi trongvar myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number db.birthdays.insert[ { dateonly: myDate, event: "This day is my birthday!" } ];
4var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number db.birthdays.insert[ { dateonly: myDate, event: "This day is my birthday!" } ];
4var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number db.birthdays.insert[ { dateonly: myDate, event: "This day is my birthday!" } ];
5 Trả về ngày hiện tại dưới dạng đối tượng ngày.var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number db.birthdays.insert[ { dateonly: myDate, event: "This day is my birthday!" } ];
4 Kết thúc đối tượng ngày với người trợ giúpvar myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number db.birthdays.insert[ { dateonly: myDate, event: "This day is my birthday!" } ];
7.var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number db.birthdays.insert[ { dateonly: myDate, event: "This day is my birthday!" } ];
7 là ở UTC.var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number db.birthdays.insert[ { dateonly: myDate, event: "This day is my birthday!" } ];
4 wraps the Date object with thevar myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number db.birthdays.insert[ { dateonly: myDate, event: "This day is my birthday!" } ];
7 helper. Thevar myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number db.birthdays.insert[ { dateonly: myDate, event: "This day is my birthday!" } ];
7 is in UTC.var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number db.birthdays.insert[ { dateonly: myDate, event: "This day is my birthday!" } ];
Bạn có thể chỉ định một ngày cụ thể bằng cách chuyển chuỗi ngày ISO-8601 với một năm trong phạm vi bao gồm
var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
9 đến // today
var myDate = localDateOnly["America/New_York"];
db.birthdays.find[ { dateonly: myDate } ];
// tomorrow
var myDate = localDateOnly[
"America/New_York",
moment[].add[ 1, "days" ]
]; // tomorrow
db.birthdays.find[ { dateonly: myDate } ];
// someone wants to know birthdays on the calendar on July 15, 2015,
// no timezone math needed, just type the date in YYYYMMDD format
db.birthdays.find[ { dateonly: 20150715 } ];
0 cho hàm tạo var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
5 hoặc hàm // today
var myDate = localDateOnly["America/New_York"];
db.birthdays.find[ { dateonly: myDate } ];
// tomorrow
var myDate = localDateOnly[
"America/New_York",
moment[].add[ 1, "days" ]
]; // tomorrow
db.birthdays.find[ { dateonly: myDate } ];
// someone wants to know birthdays on the calendar on July 15, 2015,
// no timezone math needed, just type the date in YYYYMMDD format
db.birthdays.find[ { dateonly: 20150715 } ];
2. Các chức năng này chấp nhận các định dạng sau:
3 trả về// today var myDate = localDateOnly["America/New_York"]; db.birthdays.find[ { dateonly: myDate } ]; // tomorrow var myDate = localDateOnly[ "America/New_York", moment[].add[ 1, "days" ] ]; // tomorrow db.birthdays.find[ { dateonly: myDate } ]; // someone wants to know birthdays on the calendar on July 15, 2015, // no timezone math needed, just type the date in YYYYMMDD format db.birthdays.find[ { dateonly: 20150715 } ];
7 với ngày được chỉ định.var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number db.birthdays.insert[ { dateonly: myDate, event: "This day is my birthday!" } ];
5 Chỉ định DateTime trong timezone cục bộ của máy khách và trả về// today var myDate = localDateOnly["America/New_York"]; db.birthdays.find[ { dateonly: myDate } ]; // tomorrow var myDate = localDateOnly[ "America/New_York", moment[].add[ 1, "days" ] ]; // tomorrow db.birthdays.find[ { dateonly: myDate } ]; // someone wants to know birthdays on the calendar on July 15, 2015, // no timezone math needed, just type the date in YYYYMMDD format db.birthdays.find[ { dateonly: 20150715 } ];
7 với DateTime được chỉ định trong UTC.var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number db.birthdays.insert[ { dateonly: myDate, event: "This day is my birthday!" } ];
7 Chỉ định DateTime trong UTC và trả về// today var myDate = localDateOnly["America/New_York"]; db.birthdays.find[ { dateonly: myDate } ]; // tomorrow var myDate = localDateOnly[ "America/New_York", moment[].add[ 1, "days" ] ]; // tomorrow db.birthdays.find[ { dateonly: myDate } ]; // someone wants to know birthdays on the calendar on July 15, 2015, // no timezone math needed, just type the date in YYYYMMDD format db.birthdays.find[ { dateonly: 20150715 } ];
7 với DateTime được chỉ định trong UTC.var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number db.birthdays.insert[ { dateonly: myDate, event: "This day is my birthday!" } ];
9 Chỉ định DateTime là mili giây kể từ thời đại Unix [ngày 1 tháng 1 năm 1970] và trả về ví dụ// today var myDate = localDateOnly["America/New_York"]; db.birthdays.find[ { dateonly: myDate } ]; // tomorrow var myDate = localDateOnly[ "America/New_York", moment[].add[ 1, "days" ] ]; // tomorrow db.birthdays.find[ { dateonly: myDate } ]; // someone wants to know birthdays on the calendar on July 15, 2015, // no timezone math needed, just type the date in YYYYMMDD format db.birthdays.find[ { dateonly: 20150715 } ];
7.var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number db.birthdays.insert[ { dateonly: myDate, event: "This day is my birthday!" } ];
Trong nội bộ, các đối tượng ngày được lưu trữ dưới dạng số nguyên 64 bit có chữ ký đại diện cho số mili giây kể từ kỷ nguyên Unix [ngày 1 tháng 1 năm 1970].
Không phải tất cả các hoạt động và trình điều khiển cơ sở dữ liệu đều hỗ trợ phạm vi 64 bit đầy đủ. Bạn có thể làm việc một cách an toàn với ngày với nhiều năm trong phạm vi bao gồm
var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
9 đến // today
var myDate = localDateOnly["America/New_York"];
db.birthdays.find[ { dateonly: myDate } ];
// tomorrow
var myDate = localDateOnly[
"America/New_York",
moment[].add[ 1, "days" ]
]; // tomorrow
db.birthdays.find[ { dateonly: myDate } ];
// someone wants to know birthdays on the calendar on July 15, 2015,
// no timezone math needed, just type the date in YYYYMMDD format
db.birthdays.find[ { dateonly: 20150715 } ];
0.Nếu không có tài liệu nào có
moment[ 20150715, "YYYYMMDD", true ].isValid[] // returns true & allowed to store in the database
moment[ 150715, "YYYYMMDD", true ].isValid[] // returns false, don't insert into db
3 bằng moment[ 20150715, "YYYYMMDD", true ].isValid[] // returns true & allowed to store in the database
moment[ 150715, "YYYYMMDD", true ].isValid[] // returns false, don't insert into db
4 tồn tại trong bộ sưu tập moment[ 20150715, "YYYYMMDD", true ].isValid[] // returns true & allowed to store in the database
moment[ 150715, "YYYYMMDD", true ].isValid[] // returns false, don't insert into db
5, thì thao tác sau sẽ chèn một tài liệu với trường moment[ 20150715, "YYYYMMDD", true ].isValid[] // returns true & allowed to store in the database
moment[ 150715, "YYYYMMDD", true ].isValid[] // returns false, don't insert into db
6 được đặt thành ngày hiện tại:db.products.updateOne[ { _id: 1 }, { $set: { item: "apple" }, $setOnInsert: { dateAdded: new Date[] } }, { upsert: true } ]
Mẹo
Để trả về ngày làm chuỗi, hãy sử dụng phương thức
var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
2, như trong ví dụ sau:var myDateString = Date[];
4 kết thúc các đối tượng của loại ngày với người trợ giúp var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
7; Tuy nhiên, các đối tượng vẫn còn thuộc loại ngày. wraps objects of Date type with the var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
7 helper; however, the objects remain of type Date.
Ví dụ sau sử dụng
var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
5 để trả về đối tượng ngày với DateTime UTC được chỉ định.var myDate = new Date["2016-05-18T16:00:00Z"];
Mẹo
Để trả về ngày làm chuỗi, hãy sử dụng phương thức
var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
2, như trong ví dụ sau:var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
4 kết thúc các đối tượng của loại ngày với người trợ giúp var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
7; Tuy nhiên, các đối tượng vẫn còn thuộc loại ngày.db.cakeSales.insertMany[ [ { _id: 0, type: "chocolate", orderDate: new ISODate["2020-05-18T14:10:30Z"] }, { _id: 1, type: "strawberry", orderDate: new ISODate["2021-03-20T11:30:05Z"] }, { _id: 2, type: "vanilla", orderDate: new ISODate["2021-01-15T06:31:15Z"] } ] ]
Ví dụ sau sử dụng
var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
5 để trả về đối tượng ngày với DateTime UTC được chỉ định.db.cakeSales.find[ { orderDate: { $lt: ISODate["2021-02-25T10:03:46.000Z"] } } ]
Bạn có thể chỉ định ngày là đối tượng
var myDate = localDateOnly["America/New_York"]; // today, in YYYYMMDD number
db.birthdays.insert[
{ dateonly: myDate, event: "This day is my birthday!" }
];
7.[ { _id: 0, type: 'chocolate', orderDate: ISODate["2020-05-18T14:10:30.000Z"] }, { _id: 2, type: 'vanilla', orderDate: ISODate["2021-01-15T06:31:15.000Z"] } ]