使用pymongo将JSON导入mongoDB [英] importing JSON to mongoDB using pymongo

查看:1277
本文介绍了使用pymongo将JSON导入mongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pymongo模块导入从URL中提取的JSON文件,并将其按原样发送到mongoDB.

i am trying to import a JSON file i pull from a URL and send it to mongoDB as is, using the pymongo module.

我有以下代码

#!/usr/bin/env python
import sys, urllib2, json, pymongo
from pymongo import MongoClient
myurl = "https://gist.githubusercontent.com/border/775526/raw/b921df18ba00262ab5bba8cadb3c178e1f7748f7/config.json"
response = urllib2.urlopen(myurl)
data = response.read()
connection = MongoClient('mongodb://user:password@localhost.com:27017/database')
connection.database_names()
db = connection.database
posts = db.posts
post_id = posts.insert_many(data).inserted_id

执行此操作后,出现此错误 引发TypeError(文档必须是非空列表") TypeError:文档必须为非空列表

upon executing this, i get this error raise TypeError("documents must be a non-empty list") TypeError: documents must be a non-empty list

理想情况下,我只希望能够从URL中提取json并更新mongoDB,因为此json文件每周都会更新. 谢谢

ideally, i want to just be able to pull the json from the url and update the mongoDB as this json file will be updated every week. Thanks

推荐答案

您需要将JSON转换为Python对象,然后PyMongo会将其转换为BSON以发送到MongoDB.要将JSON转换为Python对象,请使用PyMongo随附的"bson.json_util"模块:

You need to convert JSON to Python objects, which PyMongo will then convert to BSON for sending to MongoDB. To convert JSON to Python objects use the "bson.json_util" module included with PyMongo:

from bson import json_util
data = json_util.loads(response.read())

标准的Python json.loads()函数也可以使用,但是PyMongo的json_util.loads()可以更好地处理一些MongoDB特定的细节.

The standard Python json.loads() function works, too, but PyMongo's json_util.loads() handles some MongoDB-specific details better.

这篇关于使用pymongo将JSON导入mongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆