Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
How to get BSON UTC datetime value in Node / Javascript?

I'm trying to save an entry in a MongoDB time series collection, but I'm unable to store the timeField correctly, mongo is throwing the error. MongoServerError: 'blockTime' must be present and contain a valid BSON UTC datetime value How can I convert a Date object to a BSON UTC date time?

Below is the schema

import mongoose from "mongoose";

const tradeSchema = new mongoose.Schema(
  {
    blockTime: {
      type: Number,
    },
  },
  {
    timeseries: {
      timeField: "blockTime",
    },
  }
);

const Trade = mongoose.model("trade", tradeSchema);

export default Trade;

Below is the code that is generating the error

const newTrade = new Trade({
  blockTime: new Date().valueOf(),
});

await newTrade.save();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Apparently, the type of the blockTime should be Date instead of Number, when I changed the type to Date, it worked

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!