aquatic7
aquatic7

Reputation: 635

TypeError when importing json data with pymongo

I am trying to import json data from a link containing valid json data to MongoDB. When I run the script I get the following error:

TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping

What am I missing here or doing wrong?

import pymongo
import urllib.parse
import requests

replay_url = "http://live.ksmobile.net/live/getreplayvideos?"

userid = 769630584166547456

url2 = replay_url + urllib.parse.urlencode({'userid': userid}) + '&page_size=1000'

print(f"Replay url: {url2}")

raw_replay_data = requests.get(url2).json()

uri = 'mongodb://testuser:[email protected]:45687/liveme'

client = pymongo.MongoClient(uri)

db = client.get_default_database()

replays = db['replays']

replays.insert_many(raw_replay_data)

client.close()

Upvotes: 0

Views: 255

Answers (2)

Rohit Shende
Rohit Shende

Reputation: 56

You can make one field as _id for mongodb document

use the following line before insert_many

for i in raw_replay_data['data']['video_info']:
    i['_id'] = i['vid']

this will make the 'vid' field as your '_id'. Just make sure that the 'vid' is unique for all videos.

Upvotes: 1

Rohit Shende
Rohit Shende

Reputation: 56

I saw that you are getting the video information data for 22 videos.

You can use :

replays.insert_many(raw_replay_data['data']['video_info'])

for saving them

Upvotes: 1

Related Questions