コード例 #1
0
def __get_number_questions():
    client = db_utils.connect()
    db = client[skill_config.DB_NAME]

    res = db.questions.count_documents({})

    db_utils.close(client)

    return res
コード例 #2
0
def get_question(question_id):
    client = db_utils.connect()
    db = client[skill_config.DB_NAME]

    question = db.questions.find_one({'_id': ObjectId(question_id)})

    db_utils.close(client)

    return question
コード例 #3
0
def __user_exists(user_id):
    client = db_utils.connect()
    db = client[skill_config.DB_NAME]

    res = db.users.count_documents({'user_id': user_id})

    db_utils.close(client)

    return res == 1
コード例 #4
0
def __get_already_fetched_questions(user_id):
    client = db_utils.connect()
    db = client[skill_config.DB_NAME]

    res = db.users.find_one({'user_id': user_id})

    db_utils.close(client)

    return res['fetched_questions']
コード例 #5
0
def __reset_user_fetched_questions(user_id):
    client = db_utils.connect()
    db = client[skill_config.DB_NAME]

    db.users.find_one_and_update({'user_id': user_id},
                                 {'$set': {
                                     'fetched_questions': []
                                 }})

    db_utils.close(client)
コード例 #6
0
def __add_user_fetched_question(user_id, question_id):
    client = db_utils.connect()
    db = client[skill_config.DB_NAME]

    db.users.find_one_and_update({'user_id': user_id},
                                 {'$push': {
                                     'fetched_questions': question_id
                                 }})

    db_utils.close(client)
コード例 #7
0
def __get_random_question_not_fetched(already_fetched):
    client = db_utils.connect()
    db = client[skill_config.DB_NAME]

    questions = db.questions.aggregate([{
        '$match': {
            '_id': {
                '$nin': already_fetched
            }
        }
    }, {
        '$sample': {
            'size': 1
        }
    }])

    db_utils.close(client)

    return list(questions)[0]
コード例 #8
0
def __insert_new_user(user_id):
    client = db_utils.connect()
    db = client[skill_config.DB_NAME]

    db.users.insert_one({'user_id': user_id, 'fetched_questions': []})