2014-03-04 5 views
2

mongoDB의 집계 프레임 워크를 사용하여 특정 쿼리를 얻으려고합니다. $ group 및 $ addToSet 연산자가 필요하다고 생각하지만 사용할 올바른 쿼리에 대해 혼란스러워합니다. MongoDB 집계 프레임 워크 : 그룹 쿼리

기사의 모음입니다 :

/* 0 */ 
{ 
    "_id" : 4, 
    "author" : "Kevin Vanhove", 
    "book" : { 
     "order" : 500, 
     "title" : "HTML", 
     "url" : "html" 
    }, 
    "chapter" : { 
     "img" : "navChapter-logo", 
     "order" : 500, 
     "title" : "W3C", 
     "url" : "w3c" 
    }, 
    "featured" : 0, 
    "heading" : [ 
     { 
      "title" : "title1", 
      "_id" : ObjectId("53130fb8b9b9f573a877401d") 
     }, 
     { 
      "title" : "title2", 
      "_id" : ObjectId("53130fb8b9b9f573a877401c") 
     } 
    ], 
    "intro" : "Some intro text", 
    "title" : "Internet with and without the W3C", 
    "url" : "internet-with-and-without-the-W3C" 
} 

/* 1 */ 
{ 
    "_id" : 1, 
    "author" : "Kevin Vanhove", 
    "book" : { 
     "order" : 500, 
     "title" : "Javascript", 
     "url" : "javascript" 
    }, 
    "chapter" : { 
     "img" : "navChapter-logo", 
     "order" : 500, 
     "title" : "Functions", 
     "url" : "functions" 
    }, 
    "featured" : 1, 
    "heading" : [ 
     { 
      "title" : "Parts of a function", 
      "_id" : ObjectId("53130e0cd8517b65a614c1ab") 
     }, 
     { 
      "title" : "Something else", 
      "_id" : ObjectId("53130e0cd8517b65a614c1aa") 
     } 
    ], 
    "intro" : "Some intro text", 
    "title" : "A visual illustration of the JS function", 
    "url" : "a-visual-illustration-of-the-javascript-function" 
} 

/* 2 */ 
{ 
    "_id" : 2, 
    "author" : "Kevin Vanhove", 
    "book" : { 
     "order" : 500, 
     "title" : "Javascript", 
     "url" : "javascript" 
    }, 
    "chapter" : { 
     "img" : "navChapter-logo", 
     "order" : 300, 
     "title" : "Variables", 
     "url" : "variables" 
    }, 
    "featured" : 1, 
    "heading" : [ 
     { 
      "title" : "Global vs local", 
      "_id" : ObjectId("53130ea8a9dc9c28a77ea28a") 
     }, 
     { 
      "title" : "Variable hoisting", 
      "_id" : ObjectId("53130ea8a9dc9c28a77ea289") 
     }, 
     { 
      "title" : "The scope chain", 
      "_id" : ObjectId("53130ea8a9dc9c28a77ea288") 
     } 
    ], 
    "intro" : "Some intro text", 
    "title" : "How variable scope works in javascript", 
    "url" : "how-variable-scope-works-in-javascript" 
} 

/* 3 */ 
{ 
    "__v" : 0, 
    "_id" : 3, 
    "author" : "Kevin Vanhove", 
    "book" : { 
     "order" : 500, 
     "title" : "Javascript", 
     "url" : "javascript" 
    }, 
    "chapter" : { 
     "img" : "navChapter-logo", 
     "order" : 600, 
     "title" : "Functions", 
     "url" : "functions" 
    }, 
    "featured" : 0, 
    "heading" : [ 
     { 
      "title" : "title1", 
      "_id" : ObjectId("53130f60f0de2506a81e2d62") 
     }, 
     { 
      "title" : "title2", 
      "_id" : ObjectId("53130f60f0de2506a81e2d61") 
     } 
    ], 
    "intro" : "Some intro text", 
    "title" : "Javascript closures, in depth", 
    "url" : "Javascript-closure-in-depth" 
} 

나는 모든 '독특한'책과 자신의 '독특한'장 (중복 불가능)가 필요 , 그래서 난이 쿼리를 사용

01,232,200,143 :
/* 
db.articles.aggregate({$group : {_id : "$book.title", chapters:{$addToSet:"$chapter.title"}}}) 
*/ 

이 나에게이 결과를 제공

거의 완벽하지만 원하는 것입니다. 무엇 실제로 필요한 것은 이것이다 :

/* 0 */ 
{ 
    "result" : [ 
     { 
      "_id" : "Javascript", 
      "chapters" : [ 
       { 
        "img" : "navChapter-logo", 
        "order" : 600, 
        "title" : "Functions", 
        "url" : "functions" 
       }, 
       { 
        "img" : "navChapter-logo", 
        "order" : 300, 
        "title" : "Variables", 
        "url" : "variables" 
       } 
      ] 
     }, 
     { 
      "_id" : "HTML", 
      "chapters" : [ 
       { 
        "img" : "navChapter-logo", 
        "order" : 500, 
        "title" : "W3C", 
        "url" : "w3c" 
       } 
      ] 
     } 
    ], 
    "ok" : 1 
} 

그래서 내가 그들의 유일한 장에 고유 한 모든 책이 필요하지만, 나는 또한 추가 필드는 "순서"와 "URL"과 같이 추가합니다. 내가 지금 사용하고있는 쿼리는 장 제목 만 제공합니다.

업데이트 : 나는 또한 시도

: $ addToSet를 대신 $ addToSet의 "$ 장": "$의 chapter.title"...

하지만 지금이 장에 중복으로 구매하십시오. 제목 필드. 나는 'javascript'서적에서만 2 개의 별개의 장을 가져야하고 이제는 3 장 (1 개의 복제본)을 얻습니다.

+1

는'$ addToSet'는 고유 한 값을 누적 중요 그래서'$ addToSet : "$ chapter"를 시도하면 중복되는 이유는 다른 인스턴스 (예 : 'Functions'장)에서 서로 다른'order '필드를 가진 "duplicate"장이 있기 때문입니다. 다른 단어는 실제로 중복이 아닙니다. 어쨌든 제외 시키시겠습니까? – voithos

+0

예, '주문'입력란이 필요 없습니다. – kevinius

+0

무슨 val 주문 하시겠습니까? Javascript 장의 경우 하나의 문서에서 500 개를 주문하고 다른 문서에서 600 개를 주문하십시오. 결과에서 원하는 것은 무엇입니까? –

답변

1

$group을 사용하여 장 및 장별로 집계 할 수 있습니다.

db.articles.aggregate (사례 위해

{$group : {_id : {t:"$book.title",c:"$chapter.title"}, 
      img:{$first:"$chapter.img"}, 
      url:{$first:"$chapter.url"}, 
      order:{$sum:"$chapter.order"} 
}}) 

, 나는 그것을 유지와 같은 책은 "중복"장을 가로 질러을 추가했다.

관련 문제