【问题标题】:Can Pymongo pass all mongoshell commanddsPymongo可以通过所有的mongo shell命令吗
【发布时间】:2019-10-31 13:42:05
【问题描述】:

我正在尝试使用 python 来处理MongoDB。尽量简单。

我用 Pymongo 尝试了 db.getUser()

  db = client[database_name]

  db.command("getUsers")

我收到以下错误:

pymongo.errors.OperationFailure: no such command: 'getUsers'

如果有人指导我,我将不胜感激!!!

请注意,我已经设法以与上述相同的方式使用一些本机 mongoshell 命令,例如“createUser”、“dropUser”。

最好的,

迈赫迪

【问题讨论】:

    标签: mongodb pymongo mongo-shell


    【解决方案1】:

    mongo documenation 声明:

    db.getUser() wraps the usersInfo: <username> command.
    

    查看that documenation 显示我们可以使用如下命令:

    from pymongo import MongoClient
    from bson.json_util import dumps
    
    db = MongoClient()['mydatabase']
    
    # Create a test user
    db.command('createUser', 'user', pwd='password', roles=['read'])
    
    command_result = db.command(
        {
            'usersInfo': {'user': 'user', 'db': 'mydatabase'},
            'showPrivileges': True
        }
    )
    print(command_result)
    # Optionally pretty format the output
    print(dumps(command_result, indent=4))
    

    【讨论】:

      猜你喜欢
      • 2018-01-13
      • 2015-11-01
      • 2020-04-08
      • 2010-09-18
      • 2011-06-17
      • 1970-01-01
      • 2014-07-03
      • 2016-01-02
      • 1970-01-01
      相关资源
      最近更新 更多