Để chèn Date () trong MongoDB thông qua Mongo shell, hãy sử dụng cú pháp sau
var yourVariableName= new Date(year,month, day, hour, minute);
db.yourCollectionName({yourDateFieldName:yourVariableName}); Đầu tiên chúng ta hãy tạo một biến ngày
> var creatingDate = new Date(2019, 03, 29, 13, 12);
Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu:
>db.insertingDateUsingVariableDemo.insertOne({"UserName":"John","UserMessages":["Hi","Hello","Awesome"],"UserPostDate":creatingDate}); Sau đây là truy vấn để 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.insertingDateUsingVariableDemo.find().pretty();
Điều này sẽ tạo ra kết quả sau
{
"_id" : ObjectId("5c9d1b19a629b87623db1b21"),
"UserName" : "John",
"UserMessages" : [
"Hi",
"Hello",
"Awesome"
],
"UserPostDate" : ISODate("2019-04-29T07:42:00Z")
}