1
다음과 같이 나는 몽구스의 ID 년대의 배열을 가지고 : 나는이 ID를 일치하는 하위 문서를 얻기 위해 내 몽구스 함수를 호출 할 때정렬 다른 배열을 기반으로 자바 스크립트에서 배열
var ids =[
mongoose.Types.ObjectId('58c2871414cd3d209abf4fc1'),
mongoose.Types.ObjectId('58c2871414cd3d209abf4fc0'),
mongoose.Types.ObjectId('58c2871414cd3d209abf5fc0')
];
, 객체가 나에게 돌아갑니다 그것들이 데이터베이스에서 발견되는 순서대로. 위의 'ids'배열의 순서에 따라 순서를 바꿀 수 있습니까?
[ { _id: 58c2871414cd3d209abf5fc9,
companyname: 'Dell',
position:
{ _id: 58c2871414cd3d209abf5fc0,
title: 'Software Engineer',
location: 'Waterford',
start: 'May 2017',
term: 6,
description: ' Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus quis erat vitae dsit amet, consectetur adipiscing elit. Vivamus quis erat vitae dolor tempus euismod non in mi' },
{ _id: 58ca9fd6588205e9c8f01431,
companyname: 'Google',
position:
{ _id: 58c2871414cd3d209abf4fc0,
title: 'Software Engineer',
location: 'Waterford',
start: 'May 2017',
term: 6,
description: ' Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus quis erat vitae dsit amet, consectetur adipiscing elit. Vivamus quis erat vitae dolor tempus euismod non in mi' },
{ _id: 58ca9fd6588205e9c8f01431,
companyname: 'Google',
position:
{ _id: 58c2871414cd3d209abf4fc1,
title: 'Software Engineer',
location: 'Waterford',
start: 'June 2017',
term: 12,
description: ' Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus quis erat vitae dsit amet, consectetur adipiscing elit. Vivamus quis erat vitae dolor tempus euismod non in mi' },
]
어떤 방법이 있나요 재 주문 원래 배열에 따라이 다음과 같이
데이터는 나에게 반환은? 배열에 두 개의 _ids가 반환됩니다. 정렬해야하는 위치 하위 문서의 ID입니다. 누군가가 위대 할 수 있다면 정말 붙어있어!
편집 : 다음과 같이
내 Node.js를 기능은 다음과 같습니다
var ids =[mongoose.Types.ObjectId('58c2871414cd3d209abf4fc0'), mongoose.Types.ObjectId('58c2871414cd3d209abf4fc1'), mongoose.Types.ObjectId('58c2871414cd3d209abf5fc0')];
User.aggregate({ $unwind : "$position" },
{ $match: { "position._id": {
$in: ids }
}
}
, function(err, results){
// results.sort((a, b) => {
console.log(a+" "+b);
// return ids.indexOf(mongoose.Types.ObjectId(a.position._id))
// - ids.indexOf(mongoose.Types.ObjectId(b.position._id))
// })
res.send({results:results});
});
});
콘솔에 로그인 한 :
[object Object] [object Object]
[object Object] [object Object]
감사처럼 할 생각
mongoose.Types.ObjectId()
로 변경해야 ! 나는 내 코드에 그것을 통합하는 방법에 조금 붙어 있어요, 내 Node.js 데이터를 얻기위한 게시물 요청을 표시하는 내 질문을 업데이 트했습니다. 내가 잘못하고있다면 아이디어가 필요하십니까? 지금 내가 코멘트가있는 곳을 떠나면 배열은 똑같이 반환됩니다 .. 다시 한번 감사드립니다! – user
sort 함수가 주석 처리되지 않은 코드가 동일한 배열을 반환한다는 것을 의미합니까? –
예, 이상합니다. 내가 잘못한 곳이나 다른 곳에서 가지고 있다면 확실하지 않습니까? @Khaled Hamed – user