コード例 #1
0
def list_data_builtindb(event, context):
    augment_mongoclient = MongoDB()
    collection_name = "sample-collection"
    data = event['data']
    name = data.get("name", "")
    company = data.get("company", "")
    try:
        get_data = augment_mongoclient.builtin_db[collection_name].find({
            "$and": [{
                "name": {
                    '$regex': name
                }
            }, {
                "company": {
                    '$regex': company
                }
            }]
        })
    except Exception as e:
        return str(e)
    find_value = []
    if get_data is not None:
        for element in get_data:
            element.pop('_id', None)
            find_value.append(element)
        if len(find_value) == 0:
            return "no matching data found"
    else:
        return "no data found"
    return_value = json.dumps(find_value, indent=4)
    return return_value
コード例 #2
0
def insert_data_builtindb(event, context):
    augment_mongoclient = MongoDB()
    collection_name = "sample-collection"
    data = event['data']
    try:
        augment_mongoclient.builtin_db[collection_name].insert_one(data)
    except Exception as e:
        return str(e)
    data.pop('_id', None)

    return data
コード例 #3
0
def delete_data_builtindb(event, context):
    augment_mongoclient = MongoDB()
    collection_name = "sample-collection"
    data = event['data']
    try:
        delete_result = augment_mongoclient.builtin_db[
            collection_name].delete_one(data)
        if delete_result.deleted_count == 0:
            return "data not found"
    except Exception as e:
        return str(e)

    return "ok"
コード例 #4
0
def insert_many_data_builtindb(event, context):
    augment_mongoclient = MongoDB()
    collection_name = "sample-collection"
    data = event['data']
    try:
        augment_mongoclient.builtin_db[collection_name].insert_many(data)
    except Exception as e:
        return str(e)

    for element in data:
        element.pop('_id', None)
    return_data = json.dumps(data, indent=4)
    return return_data
コード例 #5
0
def get_data_builtindb(event, context):
    augment_mongoclient = MongoDB()
    collection_name = "sample-collection"
    data = event['data']
    try:
        get_data = augment_mongoclient.builtin_db[collection_name].find_one(
            data)
    except Exception as e:
        return str(e)
    if get_data is not None:
        get_data.pop('_id', None)
    else:
        return "data not found"

    return get_data
コード例 #6
0
def update_data_builtindb(event, context):
    augment_mongoclient = MongoDB()
    collection_name = "sample-collection"
    data = event['data']
    old_data = {"name": data['old_name'], "company": data['old_company']}
    new_data = {"name": data['new_name'], "company": data['new_company']}
    try:
        update_result = augment_mongoclient.builtin_db[
            collection_name].replace_one(old_data, new_data)
        if update_result.matched_count == 0:
            return "no matching data"
    except Exception as e:
        return str(e)
    new_data.pop('_id', None)
    return new_data
コード例 #7
0
def list_data_builtindb(event, context):
    augment_mongoclient = MongoDB()
    collection_name = "sample-collection"
    try:
        get_data = augment_mongoclient.builtin_db[collection_name].find()
    except Exception as e:
        return str(e)
    find_value = []
    if get_data is not None:
        for element in get_data:
            element.pop('_id', None)
            find_value.append(element)
    else:
        return "no data found"
    return_value = json.dumps(find_value, indent=4)
    return return_value
コード例 #8
0
 def setUpClass(cls):
     cls.augment_mongodb = MongoDB(endpoint="mongodb:27017")
     username = os.environ['BUILTIN_DB_USER_NAME']
     password = os.environ['BUILTIN_DB_USER_PASSWORD']
     cls.mongo_client = MongoClient('mongodb://%s:%s@mongodb:27017' % (username, password))