rock
rock

Reputation: 21

Unable to insert Jsonfile into mongodb using python (pymongo) (Error: bson.errors.InvalidDocument: key '$oid' must not start with '$')

I have a json file that needs to be inserted into MongoDB using python. In my json file I have the following snippet: filename: roles

[{
  "_id": {
    "$oid": "1a2e34r6hyhujujikijk"
  },
  "sequence": 255559,
  "name": "cruisemethods",
  "createdAt": {
    "$date": "2020-03-31T02:37:21.716Z"
  },
  "updatedAt": {
    "$date": "2020-08-27T14:04:59.333Z"
  },
  "__v": 0
}]%

When I try to insert the above json(which is in a file), it throws an error for both objectid and date: bson.errors.InvalidDocument: key '$oid' must not start with '$'

My code:

import json
from pymongo import MongoClient
client = MongoClient('URL')
Dictionarysample = {'DBname':'collectionname'};
def func(db_name, col_name):
    dbid = getattr(client,db_name)
    collection = getattr(dbid,col_name)
    with open('Filepath') as file:
        file_data = json.load(file)
        if isinstance(file_data, list):
              collection.insert_many(file_data)
        else:
              collection.insert_one(file_data)
for key in Dictionarysample:
    func(key,Dictionarysample[key])

Versions

python: Python 3.8.2
pymongo: pymongo 3.11.3
MongodAtlas: Version 4.4.3

Tried almost all the existing stackoverflow suggestions bson.errors.InvalidDocument: key '$oid' must not start with '$' trying to insert document with pymongo, but it still didn't work.

Can somebody help me on this?

pip list

Upvotes: 2

Views: 1069

Answers (1)

Joe
Joe

Reputation: 28326

MongoDB uses an extended JSON format in order to represent data types such as ObjectID that are not supported by standard JSON.

The object { "$oid": "1a2e34r6hyhujujikijk" } (that's not a valid ObjectID by the way) should be converted to an ObjectID prior to submitting to the mongod node.

MongoDB also has additional field name restrictions such as fields may not start with a dollar sign.

The standard JSON parser that you are using is not aware of the MongoDB extensions or restrictions.

You might consider bson.json_util from pymongo to handle these automatically.

Upvotes: 2

Related Questions