Hướng dẫn mongodb match date without time - ngày so khớp mongodb không tính thời gian

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 (http://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 MongoDBMongoDB 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.

  • var myDate = localDateOnly("America/New_York");  // today, in YYYYMMDD number
    db.birthdays.insert(
      { dateonly: myDate, event: "This day is my birthday!" }
    );
    
    2 Trả về ngày hiện tại dưới dạng chuỗi trong
    var myDate = localDateOnly("America/New_York");  // today, in YYYYMMDD number
    db.birthdays.insert(
      { dateonly: myDate, event: "This day is my birthday!" }
    );
    
    4
    var myDate = localDateOnly("America/New_York");  // today, in YYYYMMDD number
    db.birthdays.insert(
      { dateonly: myDate, event: "This day is my birthday!" }
    );
    
    4

  • var 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úp
    var 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 the
    var myDate = localDateOnly("America/New_York");  // today, in YYYYMMDD number
    db.birthdays.insert(
      { dateonly: myDate, event: "This day is my birthday!" }
    );
    
    7 helper. The
    var myDate = localDateOnly("America/New_York");  // today, in YYYYMMDD number
    db.birthdays.insert(
      { dateonly: myDate, event: "This day is my birthday!" }
    );
    
    7 is in UTC.

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:

  • // 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 } );
    
    3 trả về
    var myDate = localDateOnly("America/New_York");  // today, in YYYYMMDD number
    db.birthdays.insert(
      { dateonly: myDate, event: "This day is my birthday!" }
    );
    
    7 với ngày được chỉ định.

  • // 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 } );
    
    5 Chỉ định DateTime trong timezone cục bộ của máy khách và trả về
    var myDate = localDateOnly("America/New_York");  // today, in YYYYMMDD number
    db.birthdays.insert(
      { dateonly: myDate, event: "This day is my birthday!" }
    );
    
    7 với DateTime được chỉ định trong UTC.

  • // 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 Chỉ định DateTime trong UTC và trả về
    var myDate = localDateOnly("America/New_York");  // today, in YYYYMMDD number
    db.birthdays.insert(
      { dateonly: myDate, event: "This day is my birthday!" }
    );
    
    7 với DateTime được chỉ định trong UTC.

  • // 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 } );
    
    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ụ
    var myDate = localDateOnly("America/New_York");  // today, in YYYYMMDD number
    db.birthdays.insert(
      { dateonly: myDate, event: "This day is my birthday!" }
    );
    
    7.

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();

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. 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!" }
);
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")
}
]

Ngày tháng ở MongoDB là gì?

Ngày () Trả về ngày hiện tại dưới dạng chuỗi trong Mongosh.Ngày mới () trả về ngày hiện tại dưới dạng đối tượng ngày.Mongosh kết thúc đối tượng ngày với người trợ giúp isodate.Các isodate là ở UTC.returns the current date as a string in mongosh. new Date() returns the current date as a Date object. mongosh wraps the Date object with the ISODate helper. The ISODate is in UTC.

Ngày được lưu trong MongoDB như thế nào?

MongoDB sẽ lưu trữ thông tin ngày và thời gian bằng UTC trong nội bộ, nhưng có thể dễ dàng chuyển đổi sang thời gian khác tại thời điểm truy xuất khi cần thiết.using UTC internally, but can easily convert to other timezones at time of retrieval as needed.

$ Expr trong MongoDB là gì?

$ expr có thể xây dựng các biểu thức truy vấn so sánh các trường từ cùng một tài liệu trong giai đoạn Match $.Nếu giai đoạn $ Match là một phần của giai đoạn tra cứu $, $ expr có thể so sánh các trường bằng các biến LET.Xem thực hiện nhiều lần tham gia và một truy vấn con tương quan với $ Tra cứu cho một ví dụ.build query expressions that compare fields from the same document in a $match stage. If the $match stage is part of a $lookup stage, $expr can compare fields using let variables. See Perform Multiple Joins and a Correlated Subquery with $lookup for an example.

Làm cách nào để thay đổi định dạng ngày trong MongoDB?

Toán tử $ DatetoString chuyển đổi đối tượng ngày thành chuỗi và tùy chọn cho phép bạn chỉ định định dạng cho đầu ra kết quả ...
$dayOfWeek..
$dayOfMonth..
$dayOfYear..
$hour..
$isoWeek..
$isoWeekYear..
$isoDayOfWeek..
$millisecond..