MongoDB group by
如何在Mongodb中进行查询,以查找某个位置的用户进入和退出组的计数。上面的集合有许多用户可以进入或退出的位置MongoDB group by,mongodb,aggregation-framework,Mongodb,Aggregation Framework,如何在Mongodb中进行查询,以查找某个位置的用户进入和退出组的计数。上面的集合有许多用户可以进入或退出的位置 { "ActivityList" : [ { "type" : "entry", "timestamp" : Date( 1344473257320 ), "user" : { "$ref" : "userProfile", "$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } }, { "type" : "exit
{ "ActivityList" : [
{ "type" : "entry",
"timestamp" : Date( 1344473257320 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } },
{ "type" : "exit",
"timestamp" : Date( 1348792321111 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } },
{ "type" : "entry",
"timestamp" : Date( 1348881701129 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } },
{ "type" : "exit",
"timestamp" : Date( 1348942808700 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } },
{ "type" : "entry",
"timestamp" : Date( 1348957400052 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } },
{ "type" : "exit",
"timestamp" : Date( 1349024290729 ),
"user" : { "$ref" : "userProfile",
"$id" : ObjectId( "4fdeaf6fde26fd298262bb81" ) } } ],
"Loc" : { "$ref" : "location",
"$id" : ObjectId( "501cb6d4e7e9a8f958f903c5" ) },
"_id" : ObjectId( "502308a9e7e91ab176f6708e" ) }
我正在尝试这样的事情,但没有成功
您正在使用的文档结构(项目数组)将需要在
$group()
函数中进行一些操作,使用MapReduce或MongoDB 2.2中的新功能可以更轻松地完成这些操作
下面是一个使用聚合框架的示例:
db.activity.aggregate(
// Find matching documents first (can take advantage of index)
{ $match : {
Loc: DBRef("location", ObjectId("501cb6d4e7e9a8f958f903c5"))
}},
// Unwind the ActivityList array as a document stream
{ $unwind : "$ActivityList" },
// Group the stream by activity types for a user
{ $group : {
_id : "$ActivityList.user",
activity: { $push: "$ActivityList.type" }
}},
// Unwind the activity types so they can be counted
{ $unwind : "$activity" },
// Final grouping: count of action (entry or exit) for each user
{ $group : {
_id : { user: "$_id", action: "$activity" },
count: { $sum: 1 }
}}
)
其产生的结果类似于:
{
"result" : [
{
"_id" : {
"user" : DBRef("userProfile", ObjectId("4fdeaf6fde26fd298262bb81")),
"action" : "exit"
},
"count" : 6
},
{
"_id" : {
"user" : DBRef("userProfile", ObjectId("4fdeaf6fde26fd298262bb81")),
"action" : "entry"
},
"count" : 3
},
{
"_id" : {
"user" : DBRef("userProfile", ObjectId("4fdeaf6fde26fd298262bb82")),
"action" : "entry"
},
"count" : 2
},
{
"_id" : {
"user" : DBRef("userProfile", ObjectId("4fdeaf6fde26fd298262bb85")),
"action" : "entry"
},
"count" : 1
}
],
"ok" : 1
}
MongoDB的新功能------->v2.2您能展示您尝试过但未成功的代码吗?不是trolling,但这比MySQL更容易吗?mongodb的每一位支持者都告诉我,它比其他软件更容易使用MySQL@ajacian81它比MySQL(和其他SQL数据库)更容易扩展,但编写查询并不一定比其他数据库更容易(或更难)。使用MapReduce/aggregation的某些查询比相应的SQL查询更容易编写,有些则更难编写。对于基本的SELECT、INSERT、UPDATE,就编写这些查询的难度而言,我认为Mongo与SQL差不多。@Stennie:如何在javascript中迭代相同的结果?@workinPhp:在MongoDB 2.2/2.4中,聚合结果在响应文档中以内联方式返回
result
数组。如果要在JavaScript中迭代数组,可以使用array.forEach,例如:db.collection.aggregate(…).result.forEach(函数(doc){printjson(doc)})
其中printjson()
是对每个结果文档执行的操作。仅供参考,在即将发布的MongoDB 2.6版本(或dev/unstable 2.5版本)中,默认情况下,aggregate命令将提供一个基于游标的迭代器,类似于普通的find()。
{
"result" : [
{
"_id" : {
"user" : DBRef("userProfile", ObjectId("4fdeaf6fde26fd298262bb81")),
"action" : "exit"
},
"count" : 6
},
{
"_id" : {
"user" : DBRef("userProfile", ObjectId("4fdeaf6fde26fd298262bb81")),
"action" : "entry"
},
"count" : 3
},
{
"_id" : {
"user" : DBRef("userProfile", ObjectId("4fdeaf6fde26fd298262bb82")),
"action" : "entry"
},
"count" : 2
},
{
"_id" : {
"user" : DBRef("userProfile", ObjectId("4fdeaf6fde26fd298262bb85")),
"action" : "entry"
},
"count" : 1
}
],
"ok" : 1
}