How to get the BSON UTC datetime value?

Viewed 983

I am learning the timeseries Database. I have created the Database in the mongoDb as a timeseries. Now when I am inserting the document then I am getting an error

'created_at' must be present and contain a valid BSON UTC datetime value

I am not able to understand how can I get this datetime. I have tried all format known to me but still I am getting the same error.

2 Answers

try setting timezone as UTC as :

DateTimeZone zone = DateTimeZone.UTC;
DateTimeZone.setDefault(zone);

You should try something like:

const schema = Schema(
{ 
timestamp: { type: Date, default: Date.now },
name: String,  
metadata: Object 
}, 
{
timeseries: {timeField: 'timestamp',
             metaField: 'metadata',
             granularity: 'hours'}
autoCreate: false,
});

// `Test` collection will be a timeseries collection
const Test = db.model('Test', schema);

See if works for you.

Mongoose Timeseries Docs

Related