node.js Mongoose 中的哪种 SchemaType 最适合时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10006218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which SchemaType in Mongoose is Best for Timestamp?
提问by Liatz
I'm using Mongoose, MongoDB, and Node.
我正在使用猫鼬、MongoDB 和节点。
I would like to define a schema where one of its fields is a date\timestamp.
我想定义一个模式,其中一个字段是日期\时间戳。
I would like to use this field in order to return all of the records that have been updated in the last 5 minutes.
我想使用此字段返回过去 5 分钟内更新的所有记录。
Due to the fact that in Mongoose I can't use the Timestamp() method I understand that my only option is to use the following Javascript method:
由于在 Mongoose 中我不能使用 Timestamp() 方法,我知道我唯一的选择是使用以下 Javascript 方法:
time : { type: Number, default: (new Date()).getTime() }
It's probably not the most efficient way for querying a humongous DB. I would really appreciate it if someone could share a more efficient way of implementing this.
这可能不是查询庞大数据库的最有效方法。如果有人可以分享一种更有效的实现方式,我将不胜感激。
Is there any way to implement this with Mongoose and be able to use a MongoDB timestamp?
有什么方法可以用 Mongoose 实现这一点并能够使用 MongoDB 时间戳?
回答by drinchev
Edit - 20 March 2016
编辑 - 2016 年 3 月 20 日
Mongoose now support timestamps for collections.
Mongoose 现在支持集合的时间戳。
Please consider the answer of @bobbyz below. Maybe this is what you are looking for.
Original answer
原答案
Mongoose supports a Datetype (which is basically a timestamp):
Mongoose 支持一种Date类型(基本上是时间戳):
time : { type : Date, default: Date.now }
With the above field definition, any time you save a document with an unset timefield, Mongoose will fill in this field with the current time.
有了上面的字段定义,任何时候你保存一个带有未设置time字段的文档时,Mongoose 都会用当前时间填充这个字段。
回答by bobbyz
The current version of Mongoose (v4.x) has time stamping as a built-in option to a schema:
当前版本的 Mongoose (v4.x) 将时间戳作为架构的内置选项:
var mySchema = new mongoose.Schema( {name: String}, {timestamps: true} );
This option adds createdAtand updatedAtproperties that are timestamped with a Date, and which does all the work for you. Any time you update the document, it updates the updatedAtproperty. Schema Timestamps Docs.
此选项添加带有时间戳的createdAt和updatedAt属性Date,并为您完成所有工作。每次更新文档时,它都会更新updatedAt属性。架构时间戳文档。
回答by cegprakash
In case you want custom names for your createdAt and updatedAt
如果您想要为 createdAt 和 updatedAt 自定义名称
const mongoose = require('mongoose');
const { Schema } = mongoose;
const schemaOptions = {
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
};
const mySchema = new Schema({ name: String }, schemaOptions);
回答by Nikolay_R
var ItemSchema = new Schema({
name : { type: String }
});
ItemSchema.set('timestamps', true); // this will add createdAt and updatedAt timestamps
回答by Fletch
I would like to use this field in order to return all the records that have been updated in the last 5 minutes.
我想使用此字段返回过去 5 分钟内更新的所有记录。
This means you need to update the date to "now" every time you save the object. Maybe you'll find this useful: Moongoose create-modified plugin
这意味着每次保存对象时都需要将日期更新为“现在”。也许你会发现这很有用:Moongoose 创建修改插件
回答by shengsheng
First : npm install mongoose-timestamp
第一的 : npm install mongoose-timestamp
Next: let Timestamps = require('mongoose-timestamp')
下一个: let Timestamps = require('mongoose-timestamp')
Next: let MySchema = new Schema
下一个: let MySchema = new Schema
Next: MySchema.plugin(Timestamps)
下一个: MySchema.plugin(Timestamps)
Next : const Collection = mongoose.model('Collection',MySchema)
下一个 : const Collection = mongoose.model('Collection',MySchema)
Then you can use the Collection.createdAtor Collection.updatedAtanywhere your want.
然后您可以使用Collection.createdAt或Collection.updatedAt任何您想要的地方。
Created on: Date Of The Week Month Date Year 00:00:00 GMT
创建于: 星期日期 月份 日期 年份 00:00:00 GMT
Time is in this format.
时间就是这种格式。

