在 MongoDB 中存储日期/时间的最佳方式?

在 MongoDB 中存储日期/时间的最佳方式?

可以通过两种不同的方式在 MongoDB 中存储日期/时间。在第一种方法中,您可以像 JavaScript 一样使用 Date 对象。 Date 对象是 MongoDB 中存储日期/时间的最佳方式。语法如下:

new Date();登录后复制

new ISODate();登录后复制

第一种方法:

> db.ProductsInformation.insertOne({"ProductId":"Product-1","ProductDeliveryDateTime":new Date()}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ec6786fd07954a4890686") }登录后复制

> db.ProductsInformation.insertOne({"ProductId":"Product-2","ProductDeliveryDateTime":new ISODate()}); { "acknowledged" : true, "insertedId" : ObjectId("5c6ec6846fd07954a4890687") }登录后复制

> db.ProductsInformation.find().pretty();登录后复制

{ "_id" : ObjectId("5c6ec6786fd07954a4890686"), "ProductId" : "Product-1", "ProductDeliveryDateTime" : ISODate("2019-02-21T15:40:40.901Z") } { "_id" : ObjectId("5c6ec6846fd07954a4890687"), "ProductId" : "Product-2", "ProductDeliveryDateTime" : ISODate("2019-02-21T15:40:52.684Z") }登录后复制

以上就是在 MongoDB 中存储日期/时间的最佳方式?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!