Beispiel #1
0
    def test_list_collection_names(self):
        db = Database(self.client, "pymongo_test")
        db.test.insert_one({"dummy": "object"})
        db.test.mike.insert_one({"dummy": "object"})

        colls = db.list_collection_names()
        self.assertTrue("test" in colls)
        self.assertTrue("test.mike" in colls)
        for coll in colls:
            self.assertTrue("$" not in coll)

        db.systemcoll.test.insert_one({})
        no_system_collections = db.list_collection_names(
            filter={"name": {
                "$regex": r"^(?!system\.)"
            }})
        for coll in no_system_collections:
            self.assertTrue(not coll.startswith("system."))
        self.assertIn("systemcoll.test", no_system_collections)

        # Force more than one batch.
        db = self.client.many_collections
        for i in range(101):
            db["coll" + str(i)].insert_one({})
        # No Error
        try:
            db.list_collection_names()
        finally:
            self.client.drop_database("many_collections")
Beispiel #2
0
    def test_drop_collection(self):
        db = Database(self.client, "pymongo_test")

        self.assertRaises(TypeError, db.drop_collection, 5)
        self.assertRaises(TypeError, db.drop_collection, None)

        db.test.insert_one({"dummy": u"object"})
        self.assertTrue("test" in db.list_collection_names())
        db.drop_collection("test")
        self.assertFalse("test" in db.list_collection_names())

        db.test.insert_one({"dummy": u"object"})
        self.assertTrue("test" in db.list_collection_names())
        db.drop_collection(u"test")
        self.assertFalse("test" in db.list_collection_names())

        db.test.insert_one({"dummy": u"object"})
        self.assertTrue("test" in db.list_collection_names())
        db.drop_collection(db.test)
        self.assertFalse("test" in db.list_collection_names())

        db.test.insert_one({"dummy": u"object"})
        self.assertTrue("test" in db.list_collection_names())
        db.test.drop()
        self.assertFalse("test" in db.list_collection_names())
        db.test.drop()

        db.drop_collection(db.test.doesnotexist)

        if client_context.version.at_least(3, 3, 9) and client_context.is_rs:
            db_wc = Database(self.client, 'pymongo_test',
                             write_concern=IMPOSSIBLE_WRITE_CONCERN)
            with self.assertRaises(WriteConcernError):
                db_wc.drop_collection('test')
    def test_drop_collection(self):
        db = Database(self.client, "pymongo_test")

        self.assertRaises(TypeError, db.drop_collection, 5)
        self.assertRaises(TypeError, db.drop_collection, None)

        db.test.insert_one({"dummy": u"object"})
        self.assertTrue("test" in db.list_collection_names())
        db.drop_collection("test")
        self.assertFalse("test" in db.list_collection_names())

        db.test.insert_one({"dummy": u"object"})
        self.assertTrue("test" in db.list_collection_names())
        db.drop_collection(u"test")
        self.assertFalse("test" in db.list_collection_names())

        db.test.insert_one({"dummy": u"object"})
        self.assertTrue("test" in db.list_collection_names())
        db.drop_collection(db.test)
        self.assertFalse("test" in db.list_collection_names())

        db.test.insert_one({"dummy": u"object"})
        self.assertTrue("test" in db.list_collection_names())
        db.test.drop()
        self.assertFalse("test" in db.list_collection_names())
        db.test.drop()

        db.drop_collection(db.test.doesnotexist)

        if client_context.version.at_least(3, 3, 9) and client_context.is_rs:
            db_wc = Database(self.client, 'pymongo_test',
                             write_concern=IMPOSSIBLE_WRITE_CONCERN)
            with self.assertRaises(WriteConcernError):
                db_wc.drop_collection('test')
Beispiel #4
0
def get_medias_in_using(db: Database) -> set:
    medias_checklist = set()
    # Get all collections and map data
    for col in db.list_collection_names():
        data_list = db[col].aggregate([{
            "$addFields": {
                "listSrc": {
                    '$reduce': {
                        'input': "$items.example.medias",
                        'initialValue': [],
                        'in': {
                            '$concatArrays': ["$$value", "$$this.src"]
                        }
                    }
                }
            }
        }, {
            '$project': {
                'listSrc': 1
            }
        }, {
            '$match': {
                'listSrc.0': {
                    '$exists': True
                }
            }
        }])
        for d in data_list:
            for src in d['listSrc']:
                if src:
                    medias_checklist.add(os.path.split(src)[-1])

    return medias_checklist
Beispiel #5
0
    def test_create_collection(self):
        db = Database(self.client, "pymongo_test")

        db.test.insert_one({"hello": "world"})
        self.assertRaises(CollectionInvalid, db.create_collection, "test")

        db.drop_collection("test")

        self.assertRaises(TypeError, db.create_collection, 5)
        self.assertRaises(TypeError, db.create_collection, None)
        self.assertRaises(InvalidName, db.create_collection, "coll..ection")

        test = db.create_collection("test")
        self.assertTrue(u"test" in db.list_collection_names())
        test.insert_one({"hello": u"world"})
        self.assertEqual(db.test.find_one()["hello"], "world")

        db.drop_collection("test.foo")
        db.create_collection("test.foo")
        self.assertTrue(u"test.foo" in db.list_collection_names())
        self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")
    def test_create_collection(self):
        db = Database(self.client, "pymongo_test")

        db.test.insert_one({"hello": "world"})
        self.assertRaises(CollectionInvalid, db.create_collection, "test")

        db.drop_collection("test")

        self.assertRaises(TypeError, db.create_collection, 5)
        self.assertRaises(TypeError, db.create_collection, None)
        self.assertRaises(InvalidName, db.create_collection, "coll..ection")

        test = db.create_collection("test")
        self.assertTrue(u"test" in db.list_collection_names())
        test.insert_one({"hello": u"world"})
        self.assertEqual(db.test.find_one()["hello"], "world")

        db.drop_collection("test.foo")
        db.create_collection("test.foo")
        self.assertTrue(u"test.foo" in db.list_collection_names())
        self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")
Beispiel #7
0
def collection_exists(database: Database, collection_name: str) -> bool:
    """Identify whether the specified collection exists in MongoDB already.

    Arguments:
        database {Database}: the database to check for the collection's existance.
        collection_name {str}: the name of the collection to check the existance of.

    Returns:
        bool: True if the collection exists; otherwise False.
    """
    logger.debug(f"Checking whether collection exists '{collection_name}'")

    return collection_name in database.list_collection_names()
Beispiel #8
0
def printLastSession(db: Database):
    names = db.list_collection_names()
    if "instances" in names:
        instances = db["instances"]
        find = instances.find({"category": "RecentSession"})
        sort = find.sort("lastOpened.text", pymongo.DESCENDING)
        cursor = sort.limit(1)
        try:
            session = cursor.next()
            print(db.name + ": " + session["lastOpened"]["text"])
        except:
            print(db.name + ": <empty>")
        finally:
            cursor.close()
def calculate_user_storage(db: Database, user_id: ObjectId):
    """
    Get storage that the user has used
    :param db: Database instance
    :param user_id: The user's objectId
    :return: Storage size, a list of files
    """
    medias_size = 0
    file_set = set()
    # Get all collections and map data by user id
    for col in db.list_collection_names():
        data_list = db[col].aggregate([{
            '$match': {
                'userId': user_id
            }
        }, {
            "$addFields": {
                "paths": {
                    '$reduce': {
                        'input': "$items.example.medias",
                        'initialValue': [],
                        'in': {
                            '$concatArrays': ["$$value", "$$this.src"]
                        }
                    }
                }
            }
        }, {
            '$project': {
                'paths': 1
            }
        }, {
            '$unwind': '$paths'
        }])
        # Map all files' addresses
        for path_obj in data_list:
            if 'paths' in path_obj and path_obj['paths']:
                fp = path_obj['paths'][1:]
                if os.path.exists(fp) and fp not in file_set:
                    # Build status for storage
                    medias_size += os.path.getsize(fp)
                    file_set.add(fp)
    return medias_size, file_set
Beispiel #10
0
def get_timeseries_collection(
        db: Database,
        collection_name: str,
        granularity: str = "minutes") -> Tuple[Collection, bool]:
    """
    Use MongoDB Database instance db and collection_name to get and return a collection.
    If it doesn't exist, it will be created with timeseries parameters {"timeField": "time", "metaField": "meta"}.
    """
    if collection_name in db.list_collection_names():
        coll = db[collection_name]
        created = False
    else:
        logging.info(
            f"Collection {collection_name} didn't exist, creating a new one.")
        coll = db.create_collection(collection_name,
                                    timeseries={
                                        "timeField": "time",
                                        "metaField": "meta",
                                        "granularity": granularity
                                    })
        created = True
    return coll, created
Beispiel #11
0
def update_collections(db: Database, refresh: bool) -> None:
    """
    If covid or states collections don't exist in db, downloads data, creates collections, and inserts data
    :param refresh: If True, download current data and update database
    :param db: PyMongo Database in which to update collections
    """
    collections = db.list_collection_names()

    if COLL_COVID not in collections or refresh:
        if refresh:
            db[COLL_COVID].drop()
        covid_data: JSON = get_covid_data()
        add_collection(db, COLL_COVID, covid_data)
        fix_covid_data(db, COLL_COVID)

    if COLL_STATES not in collections or refresh:
        if refresh:
            db[COLL_STATES].drop()
        states_data: JSON = get_states_data()
        add_cumulative_properties(states_data)
        add_collection(db, COLL_STATES, states_data)
        fix_states_data(db, COLL_STATES)
Beispiel #12
0
    def test(cls, db: database.Database):
        for name in db.list_collection_names():
            print(name)


# def getComputedStressList(examInfo, user, shift=0, pospond=0, db=getDB()):
#     # user = examSession.getUser()
#     gmt_begin = examInfo.startDate - dt.timedelta(minutes=shift)
#     gmt_end = examInfo.endDate + dt.timedelta(minutes=pospond)
#     # convert GMT datetime to GMT timestamp
#     timestamp_begin = unix_time_secs(gmt_begin)
#     timestamp_end = unix_time_secs(gmt_end)
#
#     cs_results = list(db[user.getMatrNr() +'_hrvdata'].find({
#             '_id.timestamp':{
#                 '$gt':timestamp_begin,
#                 '$lt':timestamp_end
#             }
#     }))
#     return cs_results
#
# def getHeartRateList(examInfo, user, shift=0, pospond=0, db=getDB()):
#     # user = examSession.getUser()
#     gmt_begin = examInfo.startDate - dt.timedelta(minutes=shift)
#     gmt_end = examInfo.endDate + dt.timedelta(minutes=pospond)
#     # convert GMT datetime to GMT timestamp
#     timestamp_begin = unix_time_secs(gmt_begin)
#     timestamp_end = unix_time_secs(gmt_end)
#
#     hr_results = list(db[user.getMatrNr() + '_heartrate'].find({
#             '_id.timestamp':{
#                 '$gt':timestamp_begin,
#                 '$lt':timestamp_end
#             }
#     }))
#     return hr_results
Beispiel #13
0
def _drop_all_indices_from_collections(db: Database, names: Sequence[str]):
    for collection_name in db.list_collection_names():
        if collection_name not in names:
            continue
        collection: Collection = db[collection_name]
        collection.drop_indexes()