1
我正在嘗試使用pymongo在MongoDB文檔上執行聚合。我也使用PyCharm IDEfound BAD_CHARACTER在聚合查詢(MongoDB/pymongo)
MongoDB version = 3.4.4
Pymongo version = 3.4.0
Python version = 3.5.2
我有一個集合:PlayerDetails有看起來像這樣的文件:
{'jersey': '11',
'height': '6-1',
'displayName': 'Maurice Williams',
'lname': 'Williams',
'fname': 'Maurice',
'_id': ObjectId('590137b105488b49ace349ac'),
'team': 'IND'}
{'jersey': '12',
'height': '6-4',
'displayName': 'Tom Brady',
'lname': 'Brady',
'fname': 'Tom',
'_id': ObjectId('590137b105488b49ace34551'),
'team': 'NE'}
我寫一個Python代碼通過團隊加入兩個文件。我有這樣的下面的代碼:
Database.py代碼片段:
import pymongo
class Dbase:
def setupConnection(self):
uri = "mongodb://127.0.0.1:27017"
Mongoclient = pymongo.MongoClient(uri)
Mongoclient.drop_database('MongoProject')
database = Mongoclient['MongoProject']
return database
App.py代碼片段:
import urllib.parse
import json
import pymongo
from Database import Dbase
def main():
Dbase_Obj = Dbase()
conn = Dbase_Obj.setupConnection()
collectionPlayerDetl = conn['PlayerDetails']
####select query using Join on both Player and Team details
collectionPlayerDetl.aggregate([
{
$lookup:{
from:'TeamDetails',
localField: "team",
foreignField: "code",
as:"team_descp"}
},
{
$project:{
"team_descp.id":0}
}
[![\]][1]][1])
兩個疑問我在嘗試,我得到$語法錯誤項目和$查找 - 「聲明預計,發現BAD_CHARACTER」
請幫助。先謝謝你。