skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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"
      },
      ...
    }
    

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search