在 Mongoose 中哪种 SchemaType 最适合时间戳?

我正在使用 Mongoose、 MongoDB 和 Node。

我想定义一个模式,其中一个字段是日期时间戳。

我想使用这个字段,以便返回在过去5分钟内更新的所有记录。

由于在 Mongoose 中我不能使用 Timestamp ()方法,我知道我唯一的选择是使用以下 Javascript 方法:

time : { type: Number, default: (new Date()).getTime() }

这可能不是查询巨大数据库的最有效方法。 如果有人能分享一个更有效的实现方法,我将非常感激。

有没有什么方法可以用 Mongoose 实现这一点,并且能够使用 MongoDB 时间戳?

172277 次浏览

Edit - 20 March 2016

Mongoose now support timestamps for collections.

Please consider the answer of @bobbyz below. Maybe this is what you are looking for.

Original answer

Mongoose supports a Date type (which is basically a timestamp):

time : { type : Date, default: Date.now }

With the above field definition, any time you save a document with an unset time field, Mongoose will fill in this field with the current time.

Source: http://mongoosejs.com/docs/guide.html

I would like to use this field in order to return all the records that have been updated in the last 5 minutes.

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

The current version of Mongoose (v4.x) has time stamping as a built-in option to a schema:

var mySchema = new mongoose.Schema( {name: String}, {timestamps: true} );

This option adds createdAt and updatedAt properties that are timestamped with a Date, and which does all the work for you. Any time you update the document, it updates the updatedAt property. Schema Timestamps Docs.

var ItemSchema = new Schema({
name : { type: String }
});


ItemSchema.set('timestamps', true); // this will add createdAt and updatedAt timestamps

Docs: https://mongoosejs.com/docs/guide.html#timestamps

First : npm install mongoose-timestamp

Next: let Timestamps = require('mongoose-timestamp')

Next: let MySchema = new Schema

Next: MySchema.plugin(Timestamps)

Next : const Collection = mongoose.model('Collection',MySchema)

Then you can use the Collection.createdAt or Collection.updatedAt anywhere your want.

Created on: Date Of The Week Month Date Year 00:00:00 GMT

Time is in this format.

In case you want custom names for your createdAt and updatedAt

const mongoose = require('mongoose');
const { Schema } = mongoose;
    

const schemaOptions = {
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
};


const mySchema = new Schema({ name: String }, schemaOptions);
new mongoose.Schema({
description: {
type: String,
required: true,
trim: true
},
completed: {
type: Boolean,
default: false
},
owner: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
}
}, {
timestamps: true
});

Mongoose now supports the timestamps in schema.

const item = new Schema(
{
id: {
type: String,
required: true,
},
{ timestamps: true },
);

This will add the createdAt and updatedAt fields on each record create.

Timestamp interface has fields

  interface SchemaTimestampsConfig {
createdAt?: boolean | string;
updatedAt?: boolean | string;
currentTime?: () => (Date | number);
}

This would help us to choose which fields we want and overwrite the date format.

You can use timestamps:true along with toDateString to get created and updated date.

    const SampleSchema = new mongoose.Schema({
accountId: {
type: String,
required: true
}
}, {
timestamps: true,
get: time => time.toDateString()
});

Sample doc in Mongo DB