热门推荐
本站出品
预售广告位一处
微信号nemoon
Mogodb 时间聚类操作
作者: 发布时间:2017-07-21 15:05:26 浏览次数:788因为接下来数据操作更为复杂,如何在Mongodb进行聚类操作是核心:
查找几个范例,可供参考:
mongodb group by date 聚合查询日期 统计每天数据(信息量)
/* 1 */
{
"_id" : ObjectId("557ac1e2153c43c320393d9d"),
"msgType" : "text",
"sendTime" : ISODate("2015-06-12T11:26:26.000Z")
}
/* 2 */
{
"_id" : ObjectId("557ac1ee153c43c320393d9e"),
"msgType" : "text",
"sendTime" : ISODate("2015-06-12T11:26:38.000Z")
}
/* 3 */
{
"_id" : ObjectId("557ac2012de5d32d213963b5"),
"msgType" : "text",
"sendTime" : ISODate("2015-06-12T11:26:56.000Z")
}
/* 4 */
{
"_id" : ObjectId("557ac978bb31196e21d23868"),
"msgType" : "text",
"sendTime" : ISODate("2015-06-12T11:58:47.000Z")
}
/* 5 */
{
"_id" : ObjectId("557ac9afbb31196e21d23869"),
"msgType" : "text",
"sendTime" : ISODate("2015-06-12T11:59:43.000Z")
}
// SQL Here
db.getCollection('wechat_message').aggregate(
[
{ $project : { day : {$substr: ["$sendTime", 0, 10] }}},
{ $group : { _id : "$day", number : { $sum : 1 }}},
{ $sort : { _id : -1 }}
]
)
// Result Here
"result" : [
{
"_id" : "2015-07-06",
"number" : 13.0000000000000000
},
{
"_id" : "2015-07-05",
"number" : 3.0000000000000000
},
{
"_id" : "2015-07-03",
"number" : 10.0000000000000000
},
{
"_id" : "2015-07-02",
"number" : 29.0000000000000000
},
123
/* 18 */
{
"_id" : ObjectId("5596414cbe4d73a327e50274"),
"msgType" : "text",
"sendTime" : ISODate("2015-07-03T08:01:16.000Z")
}
/* 19 */
{
"_id" : ObjectId("5596414cbe4d73a327e50275"),
"msgType" : "text",
"sendTime" : ISODate("2015-07-03T08:01:16.000Z")
}
/* 20 */
{
"_id" : ObjectId("55964179a29582f427335cff"),
"msgType" : "text",
"sendTime" : ISODate("2015-07-03T08:02:01.000Z")
}
/* 21 */
{
"_id" : ObjectId("559641aba29582f427335d01"),
"msgType" : "text",
"sendTime" : ISODate("2015-07-03T08:02:51.000Z")
}
/* 22 */
{
"_id" : ObjectId("553ae47bd1ba636729be4106"),
"msgType" : "text",
"sendTime" : ISODate("2015-04-25T00:48:59.000Z")
}
// Group Sql here
db.getCollection('wechat_message').aggregate(
[
{ $group : { _id : {$dayOfYear: "$sendTime"}, click : { $sum : 1 }}},
{ $sort : { _id : -1 }}
]
)
// Result here
{
"_id" : 178,
"click" : 8.0000000000000000
},
{
"_id" : 177,
"click" : 21.0000000000000000
},
{
"_id" : 176,
"click" : 32.0000000000000000
},
{
"_id" : 175,
"click" : 16.0000000000000000
},
{
"_id" : 174,
"click" : 8.0000000000000000
},
{
"_id" : 173,
"click" : 7.0000000000000000
},
{
"_id" : 172,
"click" : 8.0000000000000000
},
456
// mongodb的查询真让人难以琢磨,就查询单天信息,都需要花费一番功夫才行。
// 第一种方式:
coll.aggregate([
{$project:{sendDate: {$substr: ['$sendTime', 0, 10]}, sendTime: 1, content:1}},
{$match:{sendDate: '2015-07-05'}},
])
// 第二种方式(第二种的变异):
coll.aggregate([
{$match: {'sendTime': {'$gte': new Date('2015-07-05'), '$lt': new Date('2015-07-06')}}}
// 第三中方式(第二种的变异):
coll.aggregate([
{$match: {'sendTime': {'$gte': new Date('2015-07-05 00:00:00'), '$lte': new Date('2015-07-05 23:59:59')}}}
Copyright ©2019 易一网络科技|www.yeayee.com All Right Reserved.
技术支持:自助建站 | 领地网站建设 |短信接口 版权所有 © 2005-2019 lingw.net.粤ICP备16125321号 -5 |