从 python 中的 gmaps 库将集合导入到 mongodb 服务器

Import collection to mongodb server from gmaps library in python

我正在使用 python,我正在尝试导入一个 JSON,就像我从这段代码中收到的一样:

from gmaps import Geocoding
api = Geocoding(api_key='<my key>')
api.geocode("calle tigre 129 cusco")

到mongodb服务器,就像我用这段代码创建了一个集合:

from pymongo import MongoClient
client = MongoClient('mongodb://<user>:<pass>@ds049219.mongolab.com:49219/<__>')

db = client.<__>
posts = db.post
post = [{'name': 'Joy', 'food': 'pasta'}, {'name': 'Hant', 'food': 'pizza', 'location': 'Holland'}, {'name': 'Jim', 'food': 'meat'}]

id_<__>s= posts.insert(post)
print 'create the id: %s'%post
client.close()

所以我写了那个代码:

import pymongo
from pymongo import MongoClient
from gmaps import Geocoding

api = Geocoding(api_key='<my key>')
api.geocode("calle tigre 129 cusco")

client2 =  MongoClient('mongodb://<user>:<pass>@ds049219.mongolab.com:49219/<__>')
db = client2.<__>
apis = db.api
api = [{"calle tigre 129 cusco"}]

maps_<__> = apis.insert(api)
print 'create the maps: %s'%api

client2.close()

我最后的代码哪里做错了?

谢谢

你的问题是这里的这一行:

api = [{"calle tigre 129 cusco"}]

{"calle tigre 129 cusco"} 创建集合:

>>> {"calle tigre 129 cusco"}
set(['calle tigre 129 cusco'])

PyMongo 需要一个 python 字典。所以你想要这样的东西:

api = [{"address": "calle tigre 129 cusco"}]