我的数据库中有 > 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
特定时间的所有记录。
我不知道如何添加过滤器type
并createTime
获得我想要的东西。
更新 感谢@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" },
}
}
]
)