2014-01-13 2 views
0

Meteor.js에서 필드의 고유 한 조합을 어떻게 선택할 수 있습니까? 예를 들어, 우리가있는 경우 :Meteor.js에서 필드의 고유 한 조합으로 레코드 선택

type color owner 
---- ----- ----- 
animal red  paul 
animal red  jack 
animal blue  paul 
food blue jack 

다음과 같은 결과 집합을 얻기 위해 수행해야 무엇 :

type color 
---- ----- 
animal red 
animal blue 
food blue 

내가 meteor-smart-collections 0.4.0 및 유성 0.7.0.1

답변

0

당신을 사용하고 있습니다를 얼마나 많은 문서를 가지고 있느냐에 따라 달라지는 fetch()을 수행 할 준비가되면 밑줄을 사용하면 쉽게 처리 할 수 ​​있습니다.

경고의 유일한 단어는 비교를 위해 문자열을 연결한다는 것입니다. 따라서 {type: 'foo', color: 'bar'}{type: 'foob', color: 'ar'}과 같은 쌍의 가능성이있는 경우 오류가 발생합니다. 이것은 당신이주는 예제에서는 보이지 않지만, 문제가된다면 이터레이터 함수의 구조를 변경하여 두 필드를 연결하는 것보다 조금 더 상상력이 필요한 작업 만하면됩니다. 하지만 원시 또는 객체를 반환하면 작동하지 않을 것이라고 생각합니다.

+0

더 나은 함수는'function (item) {return JSON.stringify ({type : item.type, color : item.color}); }' – sbking

+0

Cuberto 동의! – richsilv

0

Meteor의 Minimongo 드라이버가 아직 Aggregation Framework의 도우미를 포함하고 있다고 생각하지 않지만 최소한 하나의 문제에 대해서는 suggestions on how to call the underlying MongoDB aggregate command이 있습니다.

{ 
    "result" : [ 
     { 
      "_id" : { 
       "type" : "food", 
       "color" : "blue" 
      } 
     }, 
     { 
      "_id" : { 
       "type" : "animal", 
       "color" : "blue" 
      } 
     }, 
     { 
      "_id" : { 
       "type" : "animal", 
       "color" : "red" 
      } 
     } 
    ], 
    "ok" : 1 
} 
: 같을 것이다

db.things.aggregate(
    { $group: { 
     _id: { type: "$type", color: "$color" } 
    }} 
) 

결과를 :

db.things.insert([ 
    { type: 'animal', color: 'red', owner: 'paul'}, 
    { type: 'animal', color: 'red', owner: 'jack'}, 
    { type: 'animal', color: 'blue', owner: 'paul'}, 
    { type: 'food', color: 'blue', owner: 'jack'} 
]) 

당신은 MongoDB를의 Aggregation Framework$group operator를 사용하여 서버 측이 그룹화 작업을 수행 할 수 있습니다

은 데이터를 가정처럼 보인다

관련 문제