【问题标题】:Aggregation in mongoenginemongoengine 中的聚合
【发布时间】:2017-08-22 07:00:03
【问题描述】:

我尝试在 mongoengine 中使用聚合,但没有得到任何结果。初始代码:

result = Review.objects.aggregate([{'$match': {'status': 'Remind'}}])

但我得到了错误:

command SON([('aggregate', u'review'), ('pipeline', [[{'$match': {'status': 'remind'}}]]), ('cursor', {})]) on namespace db_name.$cmd failed: exception: pipeline element 0 is not an object

在参考了各种links之后,我更新了我的代码:

pipeline = [{ "$match": {'status': 'Remind' } }]
results = list(Review.objects.aggregate(*pipeline))
print 'result='+str(results)

现在,我没有收到任何异常或错误,但结果仍然为空

result=[]

这是我的示例 mongodb 数据:

{
    "_id" : ObjectId("599aaf826bc80b178a00ef1"),
    "timestamp" : "on Mar 19, 2017",
    "profile" : "Aditya Roy",
    "status" : "Remind",
    "created_on" : ISODate("2017-08-21T15:31:38.052Z")
}
{
    "_id" : ObjectId("599aaf826bc80b178a00ef2"),
    "timestamp" : "on Mar 16, 2017",
    "profile" : "Shivam Singh",
    "status" : "Remind",
    "created_on" : ISODate("2017-08-21T15:31:38.778Z")
}
{
    "_id" : ObjectId("599aaf836bc80b178a00ef3"),
    "timestamp" : "on Mar 13, 2017",
    "profile" : "Gautam Sharma",
    "status" : "Closed"
    "created_on" : ISODate("2017-08-21T15:31:39.526Z")
}

【问题讨论】:

    标签: mongodb python-2.7 mongoengine


    【解决方案1】:

    为了将来参考,以下是使用 mongoengine 的正确方法(使用 mongoengine 0.18.0 可以正常工作): Review.objects.aggregate({'$match': {'status': 'Remind'}})

    其实mongoengine的aggregate接口还是有区别的,pymongo的aggregate是带列表的

    【讨论】:

      猜你喜欢
      • 2013-11-26
      • 2014-08-16
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 2021-05-20
      • 2017-09-03
      相关资源
      最近更新 更多