Python 使用 pymongo 将 JSON 导入 mongoDB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43095786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
importing JSON to mongoDB using pymongo
提问by joseph wallberg
i am trying to import a JSON file i pull from a URL and send it to mongoDB as is, using the pymongo module.
我正在尝试使用 pymongo 模块导入我从 URL 中提取的 JSON 文件并将其按原样发送到 mongoDB。
I have the following code
我有以下代码
#!/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:[email protected]:27017/database')
connection.database_names()
db = connection.database
posts = db.posts
post_id = posts.insert_many(data).inserted_id
upon executing this, i get this error raise TypeError("documents must be a non-empty list") TypeError: documents must be a non-empty list
执行此操作后,我收到此错误 raise TypeError("document must be a non-empty list") TypeError: documents must be a non-empty list
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
理想情况下,我希望能够从 url 中提取 json 并更新 mongoDB,因为此 json 文件将每周更新。谢谢
回答by A. Jesse Jiryu Davis
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:
您需要将 JSON 转换为 Python 对象,然后 PyMongo 会将其转换为 BSON 以发送到 MongoDB。要将 JSON 转换为 Python 对象,请使用 PyMongo 附带的“bson.json_util”模块:
from bson import json_util
data = json_util.loads(response.read())
The standard Python json.loads() function works, too, but PyMongo's json_util.loads() handles some MongoDB-specific details better.
标准的 Python json.loads() 函数也可以工作,但 PyMongo 的 json_util.loads() 可以更好地处理一些 MongoDB 特定的细节。
回答by Srujan Kumar
You can also use the "requests" library:
您还可以使用“请求”库:
response = requests.get(myurl)
data = response.json()
and post that data to the database.
并将该数据发布到数据库中。