3

我的数据库中有 > 8000 条记录,这是其中之一:

{
    "_id" : ObjectId("57599c498c39598eafb781b9"),
    "_class" : "vn.cdt.entity.db.AccessLog",
    "url" : "/shop/huenguyenshop/browse",
    "ip" : "10.0.0.238",
    "sessionId" : "86E5CF8E6D465A6EDFE7C9BF7890AA4B",
    "oldSessionId" : "86E5CF8E6D465A6EDFE7C9BF7890AA4B",
    "cookie" : "{\"sessionId\":\"86E5CF8E6D465A6EDFE7C9BF7890AA4B\",\"objects\":[{\"id\":\"903815555908\",\"type\":\"VIEW_SHOP\",\"count\":1}]}",
    "isCookie" : true,
    "createTime" : NumberLong(1464935913641),
    "objectId" : "903815555908",
    "type" : "VIEW_SHOP"
}

我想做的事 :

我想找到所有oldSessionId与 (type: VIEW_ITEM type: BUY_ITEM) 相同的记录并且createTime最新的。

我尝试过的:

pipeline = ([
                {"$group" : { "_id": "$oldSessionId", "count": { "$sum": 1 } }},
                {"$match": {"count" : {"$gt": 1} } },
                {"$project": {"oldSessionId" : "$_id", "_id" : 0} }
            ])

但那只pipeline给我sessionId

    find({'createTime': {'$lt':1464419127000, '$gt':1464332727000}, 
'$or':[{'type':'BUY_ITEM'},{'type':'VIEW_ITEM'}]})

find给了我在特定时间type: VIEW_ITEM type: BUY_ITEM特定时间的所有记录。

我不知道如何添加过滤器typecreateTime获得我想要的东西。

更新 感谢@chridam 帮助我:

如果我想将特定日期添加到聚合中,我可以像这样添加查询:

 pipeline = \
    (
        [
            { "$match": {
                         "createTime": {"$lt":1464419127000, "$gt":1464332727000 },
                         "type": { "$in": ["VIEW_ITEM", "BUY_ITEM"] }
                        }
            },
            { "$sort": { "createTime": -1, "oldSessionId": 1 } },
            {
                "$group":
                    { "_id": "$oldSessionId",
                      "_class": { "$first": "$_class" },
                      "url": { "$first": "$url" },
                      "ip": { "$first": "$ip" },
                      "sessionId": { "$first": "$sessionId" },
                      "oldSessionId": { "$first": "$oldSessionId" },
                      "cookie": { "$first": "$cookie" },
                      "isCookie": { "$first": "$isCookie" },
                      "createTime": { "$first": "$createTime" },
                      "objectId": { "$first": "$objectId" },
                      "type": { "$first": "$type" },
                    }
            }

        ]

    )
4

1 回答 1

1

要获取oldSessionId与 (type:VIEW_ITEM或 type: BUY_ITEM) 相同且 createTime 是最新的所有文档,您需要执行具有以下参与者(阶段)的聚合管道显示:

  1. $match阶段:

    • 这将过滤所有类型为VIEW_ITEMor的文档BUY_ITEM。您可以将$in运算符与查询一起使用,因为它允许您选择type字段值等于指定数组中的任何值的文档,该数组恰好是具有两个可能类型值的列表,即["VIEW_ITEM", "BUY_ITEM"].
  2. $sort阶段

    • 这将提供订购前一个管道(上图)的文件。这是必要的,因为您希望在最新createTime字段上聚合这些过滤的文档。
  3. $group阶段

    • 在这个最终步骤中,您oldSessionId按键对所有已排序的文档进行分组,使用运算符添加您想要的字段$first

将上述所有管道拼接在一起形成以下聚合管道:

pipeline = [
    { "$match": {  "type": { "$in": ["VIEW_ITEM", "BUY_ITEM"] } } },
    { "$sort": { "createTime": -1, "oldSessionId": 1 } },
    {
        "$group": {
            "_id": "$oldSessionId",
            "_class": { "$first": "$_class" },
            "url": { "$first": "$url" },
            "ip": { "$first": "$ip" },
            "sessionId": { "$first": "$sessionId" },
            "cookie": { "$first": "$cookie" },
            "isCookie": { "$first": "$isCookie" },
            "createTime": { "$first": "$createTime" },
            "objectId": { "$first": "$objectId" },
            "type": { "$first": "$type" },
        }
    }
]
于 2016-06-22T07:27:37.073 回答