【发布时间】:2016-09-12 17:52:19
【问题描述】:
我是 MongoDB 和 RestFul Web 服务的新手。从下面的代码中,我从本地服务器获取结果。
import pymongo
conn=pymongo.MongoClient()
db = conn['restaurant'] # DB Name.
@csrf_exempt
@api_view(['GET'])
def getAllRestaurants(request):
rests = []
coll = db['Restaurants'] # Collection Name.
if request.method == 'GET':
all = coll.find({"isActive":True})
for each in all:
json_data = ({"_id":str(each['_id']),'name': each['name']})
rests.append(json_data)
data = {"allRestaurants": rests}
return JSONResponse(data)
GET - http://127.0.0.1:8000/restaurant/getAllRestaurants/
{ "所有餐厅": [ { "_id": "579b032ee4b048c9b19d887c", “名称”:“xxxxx” }, { "_id": "579b032ee4b048c9b19d887d", “名称”:“yyyyy” } ] }
到这里还好。
当我尝试连接远程服务器时,它给出了身份验证错误。 我知道用户名和密码。但我不知道如何在 pymongo 模块中使用以及在何处使用。 我的远程服务器也有相同的数据库名称和集合。
假设我的 login:test 和 password:test 那么如何使用 pymongo 模块连接到远程服务器。 如果我在这里错了,请纠正我。
【问题讨论】:
标签: mongodb pymongo restful-authentication