Python pymongo.collection() Examples
The following are 22
code examples of pymongo.collection().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pymongo
, or try the search function
.
Example #1
Source File: repository.py From appkernel with Apache License 2.0 | 6 votes |
def xtract(clazz_or_instance): """ Extract class name from class, removing the Service/Controller/Resource ending and adding a plural -s or -ies. :param clazz_or_instance: the class object :return: the name of the desired collection """ clazz_name = clazz_or_instance.__name__ if inspect.isclass( clazz_or_instance) else clazz_or_instance.__class__.__name__ name = re.split('Service|Controller|Resource', clazz_name)[0] if name[-2:] in ['sh', 'ch'] or name[-1:] in ['s', 'x', 'z']: name = f'{name}es' elif name[-1:] == 'y' and (name[-2:-1] in ["a", "e", "i", "o", "u"] or name[-3:-2] == 'qu'): name = f'{name[-1:]}ies' else: name = f'{name}s' return name
Example #2
Source File: mongo.py From optimade-python-tools with MIT License | 6 votes |
def __init__( self, collection: Union[ pymongo.collection.Collection, mongomock.collection.Collection ], resource_cls: EntryResource, resource_mapper: BaseResourceMapper, ): super().__init__(collection, resource_cls, resource_mapper) self.transformer = MongoTransformer(mapper=resource_mapper) self.provider_prefix = CONFIG.provider.prefix self.provider_fields = CONFIG.provider_fields.get(resource_mapper.ENDPOINT, []) self.parser = LarkParser( version=(0, 10, 1), variant="default" ) # The MongoTransformer only supports v0.10.1 as the latest grammar # check aliases do not clash with mongo operators self._check_aliases(self.resource_mapper.all_aliases()) self._check_aliases(self.resource_mapper.all_length_aliases())
Example #3
Source File: mongoclient.py From regolith with Creative Commons Zero v1.0 Universal | 6 votes |
def dump_database(self, db): """Dumps a database dict via mongoexport.""" dbpath = dbpathname(db, self.rc) os.makedirs(dbpath, exist_ok=True) to_add = [] colls = self.client[db["name"]].collection_names( include_system_collections=False ) for collection in colls: f = os.path.join(dbpath, collection + ".json") cmd = [ "mongoexport", "--db", db["name"], "--collection", collection, "--out", f, ] subprocess.check_call(cmd) to_add.append(os.path.join(db["path"], collection + ".json")) return to_add
Example #4
Source File: mongoclient.py From regolith with Creative Commons Zero v1.0 Universal | 6 votes |
def load_database(self, db: dict): """Load the database information from mongo database. It populate the 'dbs' attribute with a dictionary like {database: {collection: docs_dict}}. Parameters ---------- db : dict The dictionary of data base information, such as 'name'. """ dbs: dict = self.dbs client: pymongo.MongoClient = self.client mongodb = client[db['name']] for colname in mongodb.list_collection_names(): col = mongodb[colname] dbs[db['name']][colname] = load_mongo_col(col) return
Example #5
Source File: mongoclient.py From regolith with Creative Commons Zero v1.0 Universal | 6 votes |
def load_mongo_col(col: Collection) -> dict: """Load the pymongo collection to a dictionary. In the dictionary. The key will be the '_id' and in each value which is a dictionary there will also be a key '_id' so that the structure will be the same as the filesystem collection. Parameters ---------- col : Collection The mongodb collection. Returns ------- dct : dict A dictionary with all the info in the collection. """ return { doc['_id']: doc for doc in col.find({}) }
Example #6
Source File: repository.py From appkernel with Apache License 2.0 | 6 votes |
def create_index(collection, field_name, sort_order, unique=False): # type: (pymongo.collection.Collection, str, SortOrder, bool) -> () """ Args: collection(pymongo.collection.Collection): the collection to which the index is applied to field_name(str): the name of the document field which is being indexed sort_order(SortOrder): the sort order unique(bool): if true (false by default) it will create a unique index """ if field_name not in collection.index_information(): if isinstance(sort_order, SortOrder): direction = pymongo.ASCENDING if sort_order == SortOrder.ASC else pymongo.DESCENDING else: direction = sort_order collection.create_index( [(field_name, direction)], unique=unique, background=True, name='{}_idx'.format(field_name))
Example #7
Source File: mongoclient.py From regolith with Creative Commons Zero v1.0 Universal | 5 votes |
def insert_one(self, dbname, collname, doc): """Inserts one document to a database/collection.""" coll = self.client[dbname][collname] if ON_PYMONGO_V2: i = coll.insert(doc) return InsertOneProxy(i, True) else: return coll.insert_one(doc)
Example #8
Source File: mongo.py From optimade-python-tools with MIT License | 5 votes |
def find( self, params: Union[EntryListingQueryParams, SingleEntryQueryParams] ) -> Tuple[List[EntryResource], int, bool, set]: criteria = self._parse_params(params) all_fields = criteria.pop("fields") if getattr(params, "response_fields", False): fields = set(params.response_fields.split(",")) fields |= self.resource_mapper.get_required_fields() else: fields = all_fields.copy() results = [] for doc in self.collection.find(**criteria): results.append(self.resource_cls(**self.resource_mapper.map_back(doc))) nresults_now = len(results) if isinstance(params, EntryListingQueryParams): criteria_nolimit = criteria.copy() criteria_nolimit.pop("limit", None) data_returned = self.count(**criteria_nolimit) more_data_available = nresults_now < data_returned else: # SingleEntryQueryParams, e.g., /structures/{entry_id} data_returned = nresults_now more_data_available = False if nresults_now > 1: raise HTTPException( status_code=404, detail=f"Instead of a single entry, {nresults_now} entries were found", ) results = results[0] if results else None return results, data_returned, more_data_available, all_fields - fields
Example #9
Source File: mongo.py From optimade-python-tools with MIT License | 5 votes |
def count(self, **kwargs): for k in list(kwargs.keys()): if k not in ("filter", "skip", "limit", "hint", "maxTimeMS"): del kwargs[k] if "filter" not in kwargs: # "filter" is needed for count_documents() kwargs["filter"] = {} return self.collection.count_documents(**kwargs)
Example #10
Source File: mongo.py From optimade-python-tools with MIT License | 5 votes |
def __contains__(self, entry): return self.collection.count_documents(entry.dict()) > 0
Example #11
Source File: mongo.py From optimade-python-tools with MIT License | 5 votes |
def __len__(self): return self.collection.estimated_document_count()
Example #12
Source File: test_plugin.py From iopipe-python with Apache License 2.0 | 5 votes |
def test_trace_plugin__auto_db__pymongo( mock_send_report, handler_with_trace_auto_db_pymongo, mock_context, monkeypatch ): monkeypatch.setattr( pymongo.collection, "Collection", mongomock.collection.Collection ) monkeypatch.setattr(pymongo, "MongoClient", mongomock.MongoClient) setattr(mongomock.database.Database, "address", ("localhost", 27017)) iopipe, handler = handler_with_trace_auto_db_pymongo assert len(iopipe.config["plugins"]) == 1 handler({}, mock_context) assert len(iopipe.report.performance_entries) == 0 db_traces = iopipe.report.db_trace_entries assert len(db_traces) == 3 for db_trace in db_traces: assert db_trace["dbType"] == "mongodb" assert db_trace["request"]["hostname"] == "localhost" assert db_trace["request"]["port"] == 27017 assert db_trace["request"]["db"] == "test" assert db_trace["request"]["table"] == "my_collection" assert db_traces[0]["request"]["command"] == "insert_one" assert db_traces[2]["request"]["command"] == "update"
Example #13
Source File: mongo_transaction_attempt_list_storage_impl.py From WavesGatewayFramework with MIT License | 5 votes |
def __init__(self, collection: Collection, serializer: TransactionAttemptListSerializer, max_tries: int, limit: int = 10) -> None: TransactionAttemptListStorage.__init__(self) self._serializer = serializer self._collection = collection self._limit = limit self._max_tries = max_tries # noinspection PyMethodMayBeStatic
Example #14
Source File: mongoclient.py From regolith with Creative Commons Zero v1.0 Universal | 5 votes |
def delete_one(self, dbname, collname, doc): """Removes a single document from a collection""" coll = self.client[dbname][collname] if ON_PYMONGO_V2: return coll.remove(doc, multi=False) else: return coll.delete_one(doc)
Example #15
Source File: mongoclient.py From regolith with Creative Commons Zero v1.0 Universal | 5 votes |
def insert_many(self, dbname, collname, docs): """Inserts many documents into a database/collection.""" coll = self.client[dbname][collname] if ON_PYMONGO_V2: return coll.insert(docs) else: return coll.insert_many(docs)
Example #16
Source File: repository.py From appkernel with Apache License 2.0 | 5 votes |
def create_unique_index(collection, field_name, sort_order): MongoRepository.create_index(collection, field_name, sort_order, unique=True)
Example #17
Source File: mongoclient.py From regolith with Creative Commons Zero v1.0 Universal | 5 votes |
def all_documents(self, collname, copy=True): """Returns an iterable over all documents in a collection.""" if copy: return deepcopy(self.chained_db.get(collname, {})).values() return self.chained_db.get(collname, {}).values()
Example #18
Source File: repository.py From appkernel with Apache License 2.0 | 5 votes |
def __init__(self, connection_object: pymongo.collection.Collection, user_class, *expressions): super().__init__(*expressions) self.connection: pymongo.collection.Collection = connection_object self.user_class = user_class
Example #19
Source File: repository.py From appkernel with Apache License 2.0 | 5 votes |
def create_text_index(collection, field_name, *args): # type: (pymongo.collection.Collection, str, SortOrder, bool) -> () MongoRepository.create_index(collection, field_name, pymongo.TEXT)
Example #20
Source File: mongoclient.py From regolith with Creative Commons Zero v1.0 Universal | 5 votes |
def import_yamls(dbpath: str, dbname: str, host: str = None, uri: str = None) -> None: """Import the yaml files to mongo db. Each yaml file will be a collection in the database. The _id will be the id_key for each doc in the yaml file. Parameters ---------- dbpath : str The path to the db folder. dbname : str The name of the database in mongo. host : str The hostname or IP address or Unix domain socket path of a single mongod or mongos instance to connect to, or a mongodb URI, or a list of hostnames / mongodb URIs. uri : str Specify a resolvable URI connection string (enclose in quotes) to connect to the MongoDB deployment. """ yaml_files = itertools.chain(Path(dbpath).glob('*.yaml'), Path(dbpath).glob('*.yml')) with TemporaryDirectory() as tempd: for yaml_file in yaml_files: json_file = Path(tempd).joinpath(yaml_file.with_suffix('.json').name) loader = YAML(typ='safe') loader.constructor.yaml_constructors[u'tag:yaml.org,2002:timestamp'] = \ loader.constructor.yaml_constructors[u'tag:yaml.org,2002:str'] fsclient.yaml_to_json(str(yaml_file), str(json_file), loader=loader) import_jsons(tempd, dbname, host=host, uri=uri) return
Example #21
Source File: mongoclient.py From regolith with Creative Commons Zero v1.0 Universal | 5 votes |
def import_jsons(dbpath: str, dbname: str, host: str = None, uri: str = None) -> None: """Import the json files to mongo db. Each json file will be a collection in the database. The _id will be the same as it is in the json file. Parameters ---------- dbpath : str The path to the db folder. dbname : str The name of the database in mongo. host : str The hostname or IP address or Unix domain socket path of a single mongod or mongos instance to connect to, or a mongodb URI, or a list of hostnames / mongodb URIs. uri : str Specify a resolvable URI connection string (enclose in quotes) to connect to the MongoDB deployment. """ for json_path in Path(dbpath).glob("*.json"): cmd = ["mongoimport"] if host is not None: cmd += ['--host', host, "--db", dbname] if uri is not None: cmd += ['--uri', uri] cmd += ["--collection", json_path.stem, "--file", str(json_path)] subprocess.check_call(cmd) return
Example #22
Source File: repository.py From appkernel with Apache License 2.0 | 5 votes |
def delete_all(cls): """ deletes all documents from the collection :return: the count of deleted documents """ return cls.get_collection().delete_many({}).deleted_count