示例#1
0
    def ensure_index(self, key, unique=False, **kwargs):
        """
        Wrapper for pymongo.Collection.ensure_index
        """
        if "background" not in kwargs:
            kwargs["background"] = True

        if confirm_field_index(self.collection, key):
            return True
        else:
            try:
                self.collection.create_index(key, unique=unique, **kwargs)
                return True
            except:
                return False
示例#2
0
    def ensure_index(self, key: str, unique: Optional[bool] = False) -> bool:
        """
        Tries to create an index and return true if it suceeded
        Args:
            key: single key to index
            unique: Whether or not this index contains only unique keys

        Returns:
            bool indicating if the index exists/was created
        """

        if confirm_field_index(self._collection, key):
            return True
        else:
            try:
                self._collection.create_index(key, unique=unique, background=True)
                return True
            except Exception:
                return False
示例#3
0
    def ensure_index(self, key, unique=False):
        """
        Wrapper for pymongo.Collection.ensure_index for the files collection
        """
        # Transform key for gridfs first
        if key not in self.files_collection_fields:
            key = "metadata.{}".format(key)

        if "background" not in kwargs:
            kwargs["background"] = True

        if confirm_field_index(self.collection, key):
            return True
        else:
            try:
                self.collection.create_index(key, unique=unique, **kwargs)
                return True
            except:
                return False