2013-01-25 23 views

Trả lời

9
Collection.find({}).distinct('myField', true); 

Để sử dụng, đặt sau đây trong [Dự án] /client/lib/a.js:

LocalCollection.Cursor.prototype.distinct = function (key,random) { 
    var self = this; 

    if (self.db_objects === null) 
    self.db_objects = self._getRawObjects(true); 
    if (random) 
    self.db_objects = _.shuffle(self.db_objects); 
    if (self.reactive) 
    self._markAsReactive({ordered: true, 
          added: true, 
          removed: true, 
          changed: true, 
          moved: true}); 
    var res = {}; 
    _.each(self.db_objects,function(value){ 

    if(!res[value[key]]){ 
     res[value[key]] = value; 
    } 
    }); 
    return _.values(res); 
}; 
+0

Cảm ơn! Tôi sẽ thử cái này. – TimDog

+0

bằng cách sử dụng này tôi có thể thực hiện một tùy chỉnh 'tìm()' để làm cho hoạt động nhất định phản ứng chỉ? Vì vậy, nó chỉ phản ứng trên thêm và loại bỏ? – Akshat

+0

có. trong self._markAsReactive() thiết lập sai: đã ra lệnh, thay đổi, di chuyển. – ram1

Các vấn đề liên quan