我正在尝试使用python将2个集合与MongoDB中的另一个集合连接,此代码对我有用,但是当我在另一个集合上尝试时,结果只有1行,不应 RM_Line包含10000行 sflow包含18476321行
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["myevo"]
mycol = mydb["RM_Line"]
pipeline = [{'$lookup':
{'from' : 'sflow',
'localField' : 'IP_Adress',
'foreignField' : 'srcaddr',
'as' : 'mergeipadr'}},
{'$unwind': '$mergeipadr'}
]
mycol2 = mydb["RMLine_Merge_sfow"]
for doc in (mycol.aggregate(pipeline)):
mycol2.insert_one(doc)
////////////它给了我这个错误
PS D:\Mes documents\GI3\PFE\parse> cd 'd:\Mes documents\GI3\PFE\parse'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1'; & 'python' 'c:\Users\Hajar_PC\.vscode\extensions\ms-python.python-2019.4.11881\pythonFiles\ptvsd_launcher.py' '--default' '--client' '--host' 'localhost' '--port' '53905' 'd:\Mes documents\GI3\PFE\parse\project2.py'
PS D:\Mes documents\GI3\PFE\parse> cd 'd:\Mes documents\GI3\PFE\parse'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1'; & 'python' 'c:\Users\Hajar_PC\.vscode\extensions\ms-python.python-2019.4.11881\pythonFiles\ptvsd_launcher.py' '--default' '--client' '--host' 'localhost' '--port' '53953' 'd:\Mes documents\GI3\PFE\parse\project3.py'
Traceback (most recent call last):
File "c:\Users\Hajar_PC\.vscode\extensions\ms-python.python-2019.4.11881\pythonFiles\ptvsd_launcher.py", line 43, in <module>
main(ptvsdArgs)
File "c:\Users\Hajar_PC\.vscode\extensions\ms-python.python-2019.4.11881\pythonFiles\lib\python\ptvsd\__main__.py", line 410, in main
run()
File "c:\Users\Hajar_PC\.vscode\extensions\ms-python.python-2019.4.11881\pythonFiles\lib\python\ptvsd\__main__.py", line 291, in run_file
runpy.run_path(target, run_name='__main__')
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "d:\Mes documents\GI3\PFE\parse\project3.py", line 15, in <module>
mycol2.insert_one(doc)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\collection.py", line 700, in insert_one
session=session),
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\collection.py", line 614, in _insert
bypass_doc_val, session)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\collection.py", line 602, in _insert_one
acknowledged, _insert_command, session)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\mongo_client.py", line 1280, in _retryable_write
return self._retry_with_session(retryable, func, s, None)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\mongo_client.py", line 1233, in _retry_with_session
return func(session, sock_info, retryable)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\collection.py", line 599, in _insert_command
_check_write_command_response(result)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\helpers.py", line 217, in _check_write_command_response
_raise_last_write_error(write_errors)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\helpers.py", line 198, in _raise_last_write_error
raise DuplicateKeyError(error.get("errmsg"), 11000, error)
pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: myevo.RMLine_Merge_RMAccessPoint_Merge_sfow index: _id_ dup key: { : ObjectId('5c89bef64ce7dd4ef54ca198') }
PS D:\Mes documents\GI3\PFE\parse> cd 'd:\Mes documents\GI3\PFE\parse'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1'; & 'python' 'c:\Users\Hajar_PC\.vscode\extensions\ms-python.python-2019.4.11881\pythonFiles\ptvsd_launcher.py' '--default' '--client' '--host' 'localhost' '--port' '54743' 'd:\Mes documents\GI3\PFE\parse\project3.py'
PS D:\Mes documents\GI3\PFE\parse> cd 'd:\Mes documents\GI3\PFE\parse'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1'; & 'python' 'c:\Users\Hajar_PC\.vscode\extensions\ms-python.python-2019.4.11881\pythonFiles\ptvsd_launcher.py' '--default' '--client' '--host' 'localhost' '--port' '54778' 'd:\Mes documents\GI3\PFE\parse\project3.py'
Traceback (most recent call last):
File "c:\Users\Hajar_PC\.vscode\extensions\ms-python.python-2019.4.11881\pythonFiles\ptvsd_launcher.py", line 43, in <module>
main(ptvsdArgs)
File "c:\Users\Hajar_PC\.vscode\extensions\ms-python.python-2019.4.11881\pythonFiles\lib\python\ptvsd\__main__.py", line 410, in main
run()
File "c:\Users\Hajar_PC\.vscode\extensions\ms-python.python-2019.4.11881\pythonFiles\lib\python\ptvsd\__main__.py", line 291, in run_file
runpy.run_path(target, run_name='__main__')
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "d:\Mes documents\GI3\PFE\parse\project3.py", line 15, in <module>
mycol2.insert_one(doc)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\collection.py", line 700, in insert_one
session=session),
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\collection.py", line 614, in _insert
bypass_doc_val, session)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\collection.py", line 602, in _insert_one
acknowledged, _insert_command, session)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\mongo_client.py", line 1280, in _retryable_write
return self._retry_with_session(retryable, func, s, None)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\mongo_client.py", line 1233, in _retry_with_session
return func(session, sock_info, retryable)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\collection.py", line 599, in _insert_command
_check_write_command_response(result)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\helpers.py", line 217, in _check_write_command_response
_raise_last_write_error(write_errors)
File "C:\Users\Hajar_PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\helpers.py", line 198, in _raise_last_write_error
raise DuplicateKeyError(error.get("errmsg"), 11000, error)
pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: myevo.RMLine_Merge_sfow index: _id_ dup key: { : ObjectId('5c89bef64ce7dd4ef54ca198') }