Skip to content

Instantly share code, notes, and snippets.

@marantz
Created December 3, 2014 04:53
Show Gist options
  • Save marantz/d2a8700005139b61a7c2 to your computer and use it in GitHub Desktop.
Save marantz/d2a8700005139b61a7c2 to your computer and use it in GitHub Desktop.
{
"key" : "77-AF-9A-FA-B6-F3",
"key_type" : "MAC",
"history" : [
{
"I_DATETIME" : "20141103011530",
"USER_ID" : "test1"
},
{
"I_DATETIME" : "20141103011529",
"USER_ID" : "test2"
},
{
"I_DATETIME" : "20141103011528",
"USER_ID" : "test3"
},
{
"I_DATETIME" : "20141103011527",
"USER_ID" : "test2"
},
{
"I_DATETIME" : "20141103011526",
"USER_ID" : "test2"
}
]
}
@ppillip
Copy link

ppillip commented Dec 3, 2014

질의는 이렇습니다.

db.test.aggregate([
/여기에 doc를 걸러낼 match 조건을 넣으세요/
{$unwind:"$history"}
,{$project : {key:"$key",USER_ID:"$USER_ID"} }
,{$group:{_id:"$key" , "cnt":{$sum:1}}}

])

@marantz
Copy link
Author

marantz commented Dec 3, 2014

상기 질의는 MAC Address 의 모든 유저의 접속 카운트를 가지고 오는 것 같은데요.
음... 제가 말씀드린 것은 user 별 distinct count 를 가지고 오고 싶은 것입니다.

현재는 아래의 질의를 가지고 Java 에서 result count 를 세고 필요한 경우 해당
USER_ID 의 count 를 fetch 해서 처리 중입니다.

db.test.aggregate(
   { $unwind : "$history" }
  ,{ $match : {"key":"77-AF-9A-FA-B6-F3","key_type" : "MAC"}}
  ,{ $project : { history : { USER_ID : 1 } } }
  ,{ $group : {_id: "$history.USER_ID", "cnt" : {$sum:1} } }
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment