2017-05-17 1 views
0

아래 내 스키마입니다. 내 'file_id'(내 file_id를 정의 할 것입니다.) 다른 'user_email'.. 계산해야합니다. 비슷한 및 다른 user_email 함께 file_id 수를 메모가 없습니다. 서로 다른 user_email.any 도움말을 사용하여 내 file_id의 개수를 계산 하시겠습니까? 다음과 같이 그런 다음에야 당신은 단순히 결과를 필터링 할 수 있습니다 특정 file_id에 관심이 있다면mongoDb에서 다른 값을 찾고 그룹화하는 방법은 무엇입니까?

{ 
    "_id" : ObjectId("591bedc8c942a3514ed09b24"), 

    "file_id" : "58450c20b053dc426b935927", 
    "description" : "get your output ready", 
    "user_email" : "[email protected]", 
    "__v" : 0 
} 
{ 
    "_id" : ObjectId("591bedc8c942a3514ed09b25"), 

    "description" : "get your output ready", 
    "user_email" : "[email protected]", 
    "__v" : 0 
} 
{ 
    "_id" : ObjectId("591bedc8c942a3514ed09b26"), 

    "file_id" : "58450c20b053dc426b935927", 
    "description" : "get your output ready", 
    "user_email" : "[email protected]", 
    "__v" : 0 
} 
{ 
    "_id" : ObjectId("591bedc8c942a3514ed09b27"), 

    "file_id" : "58450c20b053dc426b935927", 
    "description" : "get your output ready", 
    "user_email" : "[email protected]", 
    "__v" : 0 
} 
{ 
    "_id" : ObjectId("591beea4a62559b4506549e0"), 

    "file_id" : "58450c20b053dc426b935927", 
    "description" : "get your output ready", 
    "user_email" : "[email protected]", 
    "__v" : 0 
} 



db.myschema.aggregate({$group:{"file_id":"58450c20b053dc426b935927","user_email" : "[email protected]",count: { $sum: 1 }} }) 
+0

귀하의 질문에 명확하지 않습니다. – Lekhnath

+0

나는 다른 사용자 이메일로 내 쿼리를 정의하고있는 파일 아이디의 개수를 가져와야합니다. 동일한 file_id 및 동일한 user_email이있는 10 개의 레코드가있는 경우 1로 계산되고 10으로 계산되어야합니다. – Jagadeesh

답변

1

다음 쿼리는 당신에게 독특한 file_id의 수와 user_email

db.myschema.aggregate([ 

{ 
    $group: { 
    _id: {"file_id": "$file_id", "user_email": "$user_email"}, 
    count: {$sum: 1} 
    } 
}, 
{ 

    $project: { 
    _id: 0, 
    "user_email": "$_id.user_email", 
    "file_id": "$_id.file_id", 
    "count": 1 
    } 

} 

]); 

그래서 편집 를 제공해야합니다 :

let fileId = '58450c20b053dc426b935927'; 

db.myschema.aggregate([ 
    { 
    $group: { 
     _id: { file_id: "$file_id", user_email: "$user_email" }, 
     count: { $sum: 1 } 
    } 
    }, 
    { 
    $match: { 
     "_id.file_id": fileId 
    } 
    }, 
    { 
    $project: { 
     _id: 0, 
     user_email: "$_id.user_email", 
     file_id: "$_id.file_id", 
     count: 1 
    } 
    } 
]); 
+0

나중에이 쿼리가 제공합니다. 이 모든 다른 레코드의 고유 한 수입니다. { "count": 12, "user_email": "[email protected]", "file_id": "58450c20b053dc426b935927"} { "count": 1, "user_email ":"[email protected] ","file_id ":"5841509cbe1c099108a887f4 "} {"count ": 2,"user_email ":"[email protected] ","file_id ":"5841509cbe1c099108a887f4 "} {count ": 1,"user_email ":"[email protected] ","file_id ":"5841509cbe1c099108a887f4 "} .... 이 레코드에서는 특정 file_id를 쿼리로 검색해야합니다. ? – Jagadeesh

+0

그것은 내가 $ match : :) 작업을했다. – Jagadeesh

+0

@ Jagadeesh 업데이 트를 참조하십시오. 예! 일치 집계 파이프 라인을 추가하여 원하는 결과를 필터링 할 수 있습니다. – Lekhnath

관련 문제