无法 运行 MongoDB 聚合命令 (OperationFailure)
Cannot run MongoDB aggregate command (OperationFailure)
我正在尝试在 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 }
我认为当 运行 指定数据库上的聚合函数时,我不需要通过给出参数再次指定集合。我在这里 (https://docs.mongodb.com/manual/reference/command/aggregate/#command-fields) 上发现有一个集合参数,所以我将函数调用更改为:
mongo.test.aggregate(aggregate="test", pipeline=self.pipeline)
现在可以正常使用了。一些文档从未提及集合参数。
我正在尝试在 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 }
我认为当 运行 指定数据库上的聚合函数时,我不需要通过给出参数再次指定集合。我在这里 (https://docs.mongodb.com/manual/reference/command/aggregate/#command-fields) 上发现有一个集合参数,所以我将函数调用更改为:
mongo.test.aggregate(aggregate="test", pipeline=self.pipeline)
现在可以正常使用了。一些文档从未提及集合参数。