我正在嘗試在 python 中使用 MongoDB 聚合管道命令(使用 PyMongo),但遇到此錯誤:pymongo.errors.OperationFailure: {aggregate: 1} is not valid for '$match'; a collection is required., full error: {'ok': 0.0, 'errmsg': "{aggregate: 1} is not valid for '$match'; a collection is required.", 'code': 73, 'codeName': 'InvalidNamespace'}我嘗試刪除第一個匹配項,但它只會將“$match”更改為“$project”。這是我正在使用的管道: [ { "$match": {"$text": {"$search": "{self.keyword}"}} }, { "$project": { "wholeDate": {"$dateFromString": {"dateString": "$date"}}, "year": {"$year": {"$dateFromString": {"dateString": "$date"}}}, } }, { "$match": {"wholeDate": {"$gte": "{self.date_from_}", "$lte": "{self.date_until_}"}} }, { "$group": { "_id": {"year": "$year"}, "count": {"$sum": 1} } } ]當我直接在 MongoDB 上運行相同的管道時,它工作得很好。工作時應該給出以下輸出:{ "_id" : { "year" : 2018 }, "count" : 34 }
1 回答

紅糖糍粑
TA貢獻1815條經驗 獲得超6個贊
我認為在指定數據庫上運行聚合函數時不需要通過給出參數來再次指定集合。我在這里(https://docs.mongodb.com/manual/reference/command/aggregate/#command-fields)發現有一個集合參數,所以我將函數調用更改為:
mongo.test.aggregate(aggregate="test", pipeline=self.pipeline)
現在效果很好。有些文檔從未提到集合參數。
添加回答
舉報
0/150
提交
取消