最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

MongoDB Error: <timeField> must be present and contain a valid BSON UTC datetime value - Stack Overflow

matteradmin8PV0评论

I am trying to import what has now become an incredibly simple JSON to MongoDB using the Import Data button in MongoDB Compass.

I started with this:

{"timestamp":"1728714859000","prio":"0"}
{"timestamp":"1728714859000","prio":"0"}

Which resulted in the import error:

 {"name":"WriteError","message":"'timestamp' must be present and contain a valid BSON UTC datetime value","index":1,"code":2,"op: {"timestamp":"1728714859000","prio":"0","_id":"67397afef3e6b1d4dc9c2f44"}}

Through trial and error, I discovered that removing the quotes from timestamp, as follows, gets me past that error.

{timestamp:"1728714859000","prio":"0"}
{timestamp:"1728714859000","prio":"0"}

Now I am presented with a new error, but only in the GUI, which states:

Failed to import with the following error:

Parser cannot parse input: expected an object key

After looking around at some other examples, I found a reference to a _id and another way to do the timestamp.

{
    "_id" : "5d11c815eb946a412ecd677d",
    "timestamp" : ISODate("2024-10-10T05:06:44.871Z"),
    "name" : "harry"
}

Now I am getting the error:

Failed to import with the following error:

Parser cannot parse input: expected a value

Following another suggestion I tried to insert data using Python:

import pymongo
import time

client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["sklogsdb"]
collection = db["sklogscol"]

record = {
    "timestamp": int(time.time() * 1000),
    "email": "[email protected]"
}

result = collection.insert_one(record)

This resulted in the same error:

pymongo.errors.WriteError: 'timestamp' must be present and contain a valid BSON UTC datetime value, full error: {'index': 0, 'code': 2, 'errmsg': "'timestamp' must be present and contain a valid BSON UTC datetime value"}

What am I doing wrong here?

I am trying to import what has now become an incredibly simple JSON to MongoDB using the Import Data button in MongoDB Compass.

I started with this:

{"timestamp":"1728714859000","prio":"0"}
{"timestamp":"1728714859000","prio":"0"}

Which resulted in the import error:

 {"name":"WriteError","message":"'timestamp' must be present and contain a valid BSON UTC datetime value","index":1,"code":2,"op: {"timestamp":"1728714859000","prio":"0","_id":"67397afef3e6b1d4dc9c2f44"}}

Through trial and error, I discovered that removing the quotes from timestamp, as follows, gets me past that error.

{timestamp:"1728714859000","prio":"0"}
{timestamp:"1728714859000","prio":"0"}

Now I am presented with a new error, but only in the GUI, which states:

Failed to import with the following error:

Parser cannot parse input: expected an object key

After looking around at some other examples, I found a reference to a _id and another way to do the timestamp.

{
    "_id" : "5d11c815eb946a412ecd677d",
    "timestamp" : ISODate("2024-10-10T05:06:44.871Z"),
    "name" : "harry"
}

Now I am getting the error:

Failed to import with the following error:

Parser cannot parse input: expected a value

Following another suggestion I tried to insert data using Python:

import pymongo
import time

client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["sklogsdb"]
collection = db["sklogscol"]

record = {
    "timestamp": int(time.time() * 1000),
    "email": "[email protected]"
}

result = collection.insert_one(record)

This resulted in the same error:

pymongo.errors.WriteError: 'timestamp' must be present and contain a valid BSON UTC datetime value, full error: {'index': 0, 'code': 2, 'errmsg': "'timestamp' must be present and contain a valid BSON UTC datetime value"}

What am I doing wrong here?

Share Improve this question edited Nov 17, 2024 at 8:09 Rowan Smith asked Nov 17, 2024 at 5:21 Rowan SmithRowan Smith 2,19019 silver badges36 bronze badges 1
  • ISODate is a function in Mongo shell mongosh it is not part of valid JSON. The easiest way would be to insert a document manually and then export it. Then you will get a valid JSON. – Wernfried Domscheit Commented Nov 17, 2024 at 7:11
Add a comment  | 

3 Answers 3

Reset to default 1

That error indicates the data is being loaded into a timeseries collection, which does require a BSON UTC datetime in the defined time field.

The BSON spec (https://bsonspec./spec.html) defines UTC datetime as:

    signed_byte(9) e_name int64     UTC datetime

where the int64 is the number of milliseconds since epoch.

In many query forms, MongoDB permits using an integer to query against a datetime, matching just the int64. However, when storing data, the type of the input is maintained.

As you may know, JSON does not have a datatype to represent a date/time, so MongoDB has defined an extended JSON format to carry additional type data to permit import/export of documents while retaining datatypes not supported by JSON.

Specific to this instance, the MongoDB extended JSON representation of a UTC Datetime is:

Canonical
{"$date": {"$numberLong": "<millis>"}}

Relaxed 
{"$date": "<ISO-8601 Date/Time Format>"}

MongoDB Atlas and Compass are able to import/export extended JSON, so you should be able to use that format.

After a lot of experimenting, I found that I could add a record using python by doing this:

record = {
    "timestamp": datetime.datetime.now(),
    "email": "[email protected]"
}

Upon looking at the record in the MongoDB database, this is the format which MongoDB had and this works when importing JSON:

{
  "timestamp": {
      "$date": "2024-11-18T20:54:41.916Z"
  },
  ...
}

The issue arises because MongoDB expects valid JSON and BSON structures during the import, and there are a few syntax and data-type issues in your input.And another thing is, Strings like "1728714859000" will not be interpreted correctly as timestamps.Hope this solves your problem

Post a comment

comment list (0)

  1. No comments so far