edwin
edwin

Reputation: 311

Python Mongodb, TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson

I am able to retrieve the JSON file from a URL and assign it to a variable to print out for testing, but I am having real trouble inserting it into the MongoDB. here's my code.

import urllib.request, json
from datetime import datetime
import pymongo
from pymongo import *
client = MongoClient()
db = client['2marte71']
posts = db.posts
with urllib.request.urlopen("http://southamptonstudentrooms.com/IoT/arduinoData.php") as url:
    data = json.loads(url.read().decode())
    print(data) # just to test json data
result = posts.insert_one(data).inserted_id

And the shell output:

[{'temp': '24', 'time': '00:35:17', 'ldr': '40', 'hum': '44'}, {'temp': '24', 'time': '00:37:21', 'ldr': '40', 'hum': '44'}, {'temp': '24', 'time': '00:39:25', 'ldr': '40', 'hum': '44'}, {'temp': '23', 'time': '00:00:13', 'ldr': '50', 'hum': '46'}, {'temp': '23', 'time': '00:02:16', 'ldr': '39', 'hum': '46'}, {'temp': '23', 'time': '00:04:20', 'ldr': '39', 'hum': '46'}, {'temp': '23', 'time': '00:06:24', 'ldr': '39', 'hum': '46'}, {'temp': '23', 'time': '00:08:28', 'ldr': '39', 'hum': '46'}, {'temp': '23', 'time': '00:10:31', 'ldr': '39', 'hum': '46'}, {'temp': '23', 'time': '00:12:35', 'ldr': '39', 'hum': '46'}, {'temp': '23', 'time': '00:14:39', 'ldr': '50', 'hum': '46'}, {'temp': '23', 'time': '00:16:43', 'ldr': '39', 'hum': '46'}, {'temp': '23', 'time': '00:18:46', 'ldr': '36', 'hum': '46'}, {'temp': '23', 'time': '00:20:50', 'ldr': '36', 'hum': '45'}, {'temp': '23', 'time': '00:22:54', 'ldr': '37', 'hum': '45'}, {'temp': '23', 'time': '00:24:58', 'ldr': '37', 'hum': '45'}, {'temp': '23', 'time': '00:27:01', 'ldr': '38', 'hum': '45'}, {'temp': '24', 'time': '00:29:05', 'ldr': '41', 'hum': '45'}, {'temp': '24', 'time': '00:31:09', 'ldr': '41', 'hum': '44'}, {'temp': '24', 'time': '00:33:13', 'ldr': '40', 'hum': '44'}]

Traceback (most recent call last):
  File "testPythonJson.py", line 11, in <module>
    result = posts.insert_one(data).inserted_id
  File "/usr/local/lib/python3.5/dist-packages/pymongo/collection.py", line 664, in insert_one
    common.validate_is_document_type("document", document)
  File "/usr/local/lib/python3.5/dist-packages/pymongo/common.py", line 409, in validate_is_document_type
    "collections.MutableMapping" % (option,))
TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping

Upvotes: 3

Views: 9482

Answers (1)

Robert Moskal
Robert Moskal

Reputation: 22553

Easy peasy. You're trying to insert an array into mongo. Either wrap the array in a top level dictionary to insert a single document with your array as a property:

data = {data:data}
result = posts.insert_one(data).inserted_id 

or use one of the functions that allows for multiple documents to be inserted at once:

posts.insert_many(data)

Then each array item will be a separate document.

Upvotes: 4

Related Questions