2016-06-03 3 views
0

이 mongoose-query를 시도했는데 왜이 정렬이 작동하지 않는지 알지 못합니다. (stackoverflow에서 구문을 찾았으므로 아무 생각도하지 못했습니다. exec를 find, doesnt work 중 하나로 대체하려고했습니다. Mongoose find-query : 날짜순으로 JSON 배열 정렬

var messageSchema = new Schema({ 
    id: Number, 
    name: Schema.Types.Mixed, 
    type: Schema.Types.Mixed, 
    message: 
     { 
      messagetype: String, 
      timestamp: Number, 
      messagestatus: String 
     } 
}, {collection: 'test'}); 

그렇지 message.timestamp하여 정렬 할 수도 있지만이 방법을 모르고 .. 미리 감사가 없습니다 :

model.find({'id':req.params.id}).sort({date:-1}).limit(10).exec(function(err, jsonarray){...}; 

내 스키마와 jsondata은 같다.

그런 식으로 시도해 보았습니다. 나는 붙어 있습니다.

json.sort(function(a, b) { 
return parseFloat(a.message.timestamp) - parseFloat(b.message.timestamp); 
}); 

업데이트 : 데이터는 다음과 같습니다

[ { id: 215456740, 
    name: 'Max', 
    type: 'freezer', 
    _id: 57513933c25d06630a3ef887, 
    __v: 0, 
    message: 
    { messagetype: 'Standard message', 
    timestamp: 1464940851375, 
    messagestatus: 'red' 
    } 
}, 
{ id: 215456740, 
name: 'Tobi', 
type: 'center', 
_id: 5751393bc25d06630a3ef888, 
__v: 0, 
    message: 
{ messagetype: 'Standard message', 
    timestamp: 1464940859265, 
    messagestatus: 'white' 
    } 
}] 

예, 그것의 의도, 여러 메시지 하나 이드. 이것은 쿼리가 존재하지 않는 특성 date에 정렬되어 출력

답변

0

.sort을 ({_ ID : -1})

날짜가 포함되어 있으므로 자동 생성 된 _id가 사용됩니다. 나중에 일했다.

0

입니다 대신

.sort({ date : -1 }) 

,이 시도 :

.sort({ 'message.timestamp' : -1 }) 
+0

시도했지만, 슬프게도 작동하지 않습니다. 매개 변수를 가져 오는 것처럼 느껴지지만 거기에서 정렬하지 않거나 새 배열을 다시로드하지 않습니다. – LinkM

+0

단지 'id'속성이 같은 여러 문서를 가지고 있다는 것이 _ 의도적입니다. 질의에서 질문에 대한 결과 목록 (수정되거나 압축 됨, 단지'message' 속성만으로도 충분 함)을 추가 할 수 있습니까? – robertklep

+0

도움을 주시면 더 나은 견해로 업데이트했습니다. – LinkM