【问题标题】:pymongo authentication user credentialspymongo 身份验证用户凭据
【发布时间】: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:testpassword:test 那么如何使用 pymongo 模块连接到远程服务器。 如果我在这里错了,请纠正我。

【问题讨论】:

    标签: mongodb pymongo restful-authentication


    【解决方案1】:
    db.authenticate('test', 'test')
    

    另见pymongos documentation

    【讨论】:

      猜你喜欢
      • 2021-12-19
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 2016-11-12
      相关资源
      最近更新 更多