
    -h                         S SK r S SKrS SKJrJrJrJrJrJr  S SK	J
r
  S SKJr  \R                  " \5      r " S S\5      rg)    N)AsyncIteratorDictIteratorListOptionalSequence)Document)
BaseLoaderc                   
   \ rS rSrSrSSSSS.S\S\S\S	\\   S
\\\      S\\\      S\	SS4S jjr
S\\   4S jrS\\   4S jrS\\   4S jrS\\   4S jrS\S\4S jrS\\   4S jr SS\S\\   S\S\4S jjrSrg)MongodbLoader   z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                    SSK Jn  U(       d  [        S5      eU(       d  [        S5      eU(       d  [        S5      eU" U5      U l        X l        X0l        U=(       d    / U l        U=(       d    0 U l        U=(       d    / U l	        Xpl
        U R                  R                  U5      U l        U R                  R                  U5      U l        g! [         a  n	[        S5      U	eSn	A	ff = f)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
             \/root/34ku/venv/lib/python3.13/site-packages/langchain_community/document_loaders/mongodb.py__init__MongodbLoader.__init__   s    @	> !BCC899@AA():;.&,".4",21R.++**73''00A/  	T	s   C 
C)C$$C)c                 J    [         R                  " U R                  5       5      $ )aO  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"   loadMongodbLoader.loadJ   s     {{4::<((    c              #   :  #     [         R                  " 5       nU R                  5       n  UR                  UR                  5       5      nUv   M&  ! [         a.    [         R                  " 5       n[         R                  " U5         Nnf = f! [         a     gf = f7f)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_loadMongodbLoader.lazy_loadX   s     	/ 113J
 //+%889R9R9TU   	/ //1J"":.	/ & sJ   BA B#B B5BBBB
BBBBc                   #    U R                  5       nU R                  R                  U R                  U5        Sh  vN nU R	                  U5      7v   M   N
 g7f)zuAsynchronously 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"   r1   MongodbLoader.alazy_loadv   sO      //1
--d.B.BJO 	.#((--	.Os&   7AAAAAAAc                   #    / nU R                   R                  U R                  5      I Sh  vN nU R                  5       nU R                   R	                  U R                  U5        Sh  vN nUR                  U R                  U5      5        M+   Ne N(
 [        U5      U:w  a%  [        R                  S[        U5       SU S35        U$ 7f)z0Asynchronously loads data into Document objects.Nz6Only partial collection of documents returned. Loaded z docs, expected .)
r   count_documentsr   r;   r<   appendr=   lenloggerwarning)r    result
total_docsr>   r?   s        r"   r(   MongodbLoader.aload   s     ??::4;O;OPP
//1
--d.B.BJO 	7#MM$0056 Q	7O v;*$NNf+&6zl!E
 s3   +CB:C(B,B-B0$CB7Cr?   c                    U R                  XR                  SS9nU R                  (       a(  UR                  U R                  U R
                  S.5        U R                  bU  U R                  XR                  SS9nUR                  5        Vs/ s H  n[        U5      PM     nnSR                  U5      nO[        U5      n[        XbS9$ s  snf )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?   rQ   fieldsvaluetextstexts          r"   r=   MongodbLoader._process_document   s     ''-@-@"'M 11OO $"&"6"6 '))#/?/?)LF-3]]_=_ESZ_E=88E?Ds8DT== >s   C
c                     [        U R                  5      =(       d    / n[        U R                  5      =(       d    / nX-   nU(       a  U Vs0 s H  oDS_M     sn$ S$ s  snf )zmConstructs 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"   r;   #MongodbLoader._construct_projection   sW     4++,2d1128b 1
5?j1jUqj1ITI1s   Ar7   rW   rM   c                     0 nU=(       d    /  HK  nUnUR                  S5       H  nUR                  Xs5      nXc:X  d  M    O   UR                  SS5      nXdU'   MM     U$ )zAExtracts and returns values for specified fields from a document.rB   _)splitgetreplace)	r    r7   rW   rM   	extractedr`   rX   keynew_field_names	            r"   rR   MongodbLoader._extract_fields   sj     	\r\EE{{3'		#/# ( #]]34N(-n% " r+   )	r   r   r   r   r   r   r   r   r   )rL   )__name__
__module____qualname____firstlineno____doc__rU   r   r   r   boolr#   r   r	   r)   r   r8   r   r1   r(   r=   r;   rR   __static_attributes__ 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
   	getLoggerrk   rF   r   rr   r+   r"   <module>rx      s5      J J - @			8	$zJ zr+   