
    3fi                         d dl Z d dlZd dlmZmZmZmZmZmZ d dl	m
Z
 d dlmZ  ej                  e      Z G d de      Zy)    N)AsyncIteratorDictIteratorListOptionalSequence)Document)
BaseLoaderc                       e Zd ZdZddddddedededee   d	eee      d
eee      deddfdZ	de
e   fdZdee   fdZdee   fdZde
e   fdZdedefdZdee   fdZ	 ddedee   dedefdZy)MongodbLoaderzLoad MongoDB documents.NT)filter_criteriafield_namesmetadata_names!include_db_collection_in_metadataconnection_stringdb_namecollection_namer   r   r   r   returnc                   	 ddl m} |st        d      |st        d      |st        d       ||      | _        || _        || _        |xs g | _        |xs i | _        |xs g | _	        || _
        | j                  j                  |      | _        | j                  j                  |      | _        y# t        $ r}	t        d      |	d}	~	ww xY w)a  
        Initializes the MongoDB loader with necessary database connection
        details and configurations.

        Args:
            connection_string (str): MongoDB connection URI.
            db_name (str):Name of the database to connect to.
            collection_name (str): Name of the collection to fetch documents from.
            filter_criteria (Optional[Dict]): MongoDB filter criteria for querying
            documents.
            field_names (Optional[Sequence[str]]): List of field names to retrieve
            from documents.
            metadata_names (Optional[Sequence[str]]): Additional metadata fields to
            extract from documents.
            include_db_collection_in_metadata (bool): Flag to include database and
            collection names in metadata.

        Raises:
            ImportError: If the motor library is not installed.
            ValueError: If any necessary argument is missing.
        r   )AsyncIOMotorClientzBCannot import from motor, please install with `pip install motor`.Nz#connection_string must be provided.zdb_name must be provided.z!collection_name must be provided.)motor.motor_asyncior   ImportError
ValueErrorclientr   r   r   r   r   r   get_databasedbget_collection
collection)
selfr   r   r   r   r   r   r   r   es
             j/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/document_loaders/mongodb.py__init__zMongodbLoader.__init__   s    @	> !BCC899@AA():;.&,".4",21R.++**73''00A/  	T	s   B3 3	C<CCc                 H    t        j                  | j                               S )a  Load data into Document objects.

        Attention:

        This implementation starts an asyncio event loop which
        will only work if running in a sync env. In an async env, it should
        fail since there is already an event loop running.

        This code should be updated to kick off the event loop from a separate
        thread if running within an async context.
        )asynciorunaload)r   s    r!   loadzMongodbLoader.loadJ   s     {{4::<((    c              #   .  K   	 t        j                         }| j                         }	 	 |j                  |j                               }| %# t        $ r, t        j                         }t        j                  |       Y jw xY w# t        $ r Y yw xY ww)a  A lazy loader for MongoDB documents.

        Attention:

        This implementation starts an asyncio event loop which
        will only work if running in a sync env. In an async env, it should
        fail since there is already an event loop running.

        This code should be updated to kick off the event loop from a separate
        thread if running within an async context.

        Yields:
            Document: A document from the MongoDB collection.
        N)	r$   get_running_loopRuntimeErrornew_event_loopset_event_loop
alazy_loadrun_until_complete	__anext__StopAsyncIteration)r   
event_loopasync_generatordocuments       r!   	lazy_loadzMongodbLoader.lazy_loadX   s     	/ 113J
 //+%889R9R9TU   	/ //1J"":.	/ & sJ   BA B#B B2B BBB	BBBBc                   K   | j                         }| j                  j                  | j                  |      2 3 d{   }| j	                  |       7 6 yw)zAsynchronously yields Document objects one at a time.

        Yields:
            Document: A document from the MongoDB collection.
        N)_construct_projectionr   findr   _process_document)r   
projectiondocs      r!   r.   zMongodbLoader.alazy_loadv   sV      //1
--d.B.BJO 	. 	.#((--	.Os&   7AAAAAAAc                   K   g }| j                   j                  | j                         d{   }| j                         }| j                   j	                  | j                  |      2 3 d{   }|j                  | j                  |             *7 d7 '6 t        |      |k7  r%t        j                  dt        |       d| d       |S w)z0Asynchronously loads data into Document objects.Nz6Only partial collection of documents returned. Loaded z docs, expected .)
r   count_documentsr   r7   r8   appendr9   lenloggerwarning)r   result
total_docsr:   r;   s        r!   r&   zMongodbLoader.aload   s     ??::4;O;OPP
//1
--d.B.BJO 	7 	7#MM$0056 Q	7O v;*$NNf+&6zl!E
 s3   +CB:C(B,B-B0#CB6Cr;   c                    | j                  || j                  d      }| j                  r(|j                  | j                  | j
                  d       | j                  V| j                  || j                  d      }|j                         D cg c]  }t        |       }}dj                  |      }nt        |      }t        ||      S c c}w )zProcess a single MongoDB document into a Document object.

        Args:
            doc: The MongoDB document dictionary to process into a Document object.
         )default)databaser    )page_contentmetadata)_extract_fieldsr   r   updater   r   r   valuesstrjoinr	   )r   r;   rK   fieldsvaluetextstexts          r!   r9   zMongodbLoader._process_document   s     ''T-@-@"'M 11OO $"&"6"6 '))#t/?/?)LF-3]]_=ESZ=E=88E?Ds8DTH== >s   Cc                     t        | j                        xs g }t        | j                        xs g }||z   }|r|D ci c]  }|d c}S dS c c}w )zuConstructs the projection dictionary for MongoDB query based
        on the specified field names and metadata names.   N)listr   r   )r   r   r   
all_fieldsfields        r!   r7   z#MongodbLoader._construct_projection   sW     4++,2d1128b >1
5?j1Uq1ITI1s   
Ar4   rQ   rG   c                     i }|xs g D ]J  }|}|j                  d      D ]  }|j                  ||      }||k(  s n |j                  dd      }|||<   L |S )zAExtracts and returns values for specified fields from a document.r=   _)splitgetreplace)	r   r4   rQ   rG   	extractedrY   rR   keynew_field_names	            r!   rL   zMongodbLoader._extract_fields   sx     	\r 	.EE{{3' 		#w/G# #]]34N(-In%	. r(   )rF   )__name__
__module____qualname____doc__rO   r   r   r   boolr"   r   r	   r'   r   r5   r   r.   r&   r9   r7   rL    r(   r!   r   r      s!   ! +//326269B9B 9B 	9B "$9B hsm,9B !#/9B ,09B 
9Bv)d8n )8H- <	.-"9 	.T(^ $>T >h >6Jx~ J 	  	
 
r(   r   )r$   loggingtypingr   r   r   r   r   r   langchain_core.documentsr	   )langchain_community.document_loaders.baser
   	getLoggerrb   rA   r   rg   r(   r!   <module>rm      s7      J J - @			8	$zJ zr(   