tornado-motor

how to run db.fsyncLock() with Motor


Is it possible to run the db.fsyncLock() command through Motor? I want my application to make a backup of the database and need to flush and lock the files before I make the copy.


Solution

  • The mongo shell executes fsyncLock by calling a MongoDB command:

    https://docs.mongodb.com/manual/reference/method/db.fsyncLock/

    As that page shows, the shell provides a simple wrapper around a fsync database command with the following syntax:

    { fsync: 1, lock: true }
    

    So you can run it with Motor as any MongoDB command:

    await client.admin.command(SON([('fsync', 1), ('lock', true)]))
    

    Here, "client" is a MotorClient. Use "await" if you're in a native coroutine defined with "async def", or use "yield" if you're in a generator-based coroutine decorated with "@gen.coroutine".