[英]Does mongoengine support lookup in aggregate method?
Im currently using mongodb version v3.0我目前使用的是 mongodb 版本 v3.0
this is my code:这是我的代码:
{'$lookup': {
'from': 'Matrix',
'localField': 'account_id',
'foreignField': 'account_id',
'as': 'Matrix'
}
}
Im having this error:我有这个错误:
Exception calling application: exception: Unrecognized pipeline stage name: '$lookup'
异常调用应用程序:异常:无法识别的管道阶段名称:'$lookup'
Query using the aggregation framework with PyMongo.This requires two connections to MongoDB (one for PyMongo to perform the aggregation query, and a second for the regular query or insert or updating via MongoEngine).使用带有 PyMongo 的聚合框架进行查询。这需要到 MongoDB 的两个连接(一个用于 PyMongo 执行聚合查询,另一个用于常规查询或通过 MongoEngine 插入或更新)。
But _get_collection() resolve this problem.但是 _get_collection() 解决了这个问题。
In below example, we use two model Plans and Addons and in both relation is quote_id在下面的例子中,我们使用了两个模型计划和插件,并且在这两个关系中都是quote_id
collection = Plans._get_collection() collection = Plans._get_collection()
pipeline = [
{
"$lookup":
{
"from":"addons",
"localField":"plans.quote_id",
"foreignField":"addons.quote_id",
"as": "addons_docs"
}
},
{
"$match":{
"addons_docs":{
"$ne":[]
}
}
},
{
"$addFields":
{
"addons_docs":
{
"$arrayElemAt":["$addons_docs",0]
}
}
},
{
"$replaceRoot":
{
"newRoot":
{
"$mergeObjects":["$addons_docs","$$ROOT"]
}
}
},
{
"$project":
{
"addons_docs":0
}
},
{
"$sort":
{
"_id":-1
}
},
{
"$limit":100
}
]
cursor = collection.aggregate(pipeline)
try:
for doc in cursor:
print(doc)
finally:
cursor.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.