2012-08-23 7 views
1

나는 Nodejs와 mongodb에 초보자입니다.

다음 시나리오에서 데이터를 가져 오려고했습니다. 프로필 스키마

var ProfileSchema = new Schema({ 
username: {type: String, match: /^[a-zA-Z0-9_.-]+$/, unique: true}, 
name: String}); 

게시물 스키마를 다음과 같이 3 스키마를 고려

var PostsSchema = new Schema({ 
profileid: {type: ObjectId, ref: 'Profile'}, 
message: {type: String, match: /^.{1,160}$/} }); 

스키마 트위터 계층 구조와 유사하다

var FollowSchema = new Schema({ 
profileid: {type: ObjectId, ref: 'Profile'}, 
followingid: {type: ObjectId, ref: 'Profile'}}); 

을 따릅니다.

내 추종자의 모든 게시물을 얻기 위해 나는 다음

Follows.find({'profileid' : '500d18823e792d8814000001'}).select('followingid -_id').limit(20).sort({addedon: 'desc'}).execFind(function (arr,followings) { 
    Posts.find({profileid: {$in: followings}}).limit(20).execFind(function (arr,data) { 
     console.log(data); 
    }); 
}); 

을 시도하지만 작동하지 않습니다. 이 문제를 해결하는 가장 좋은 방법을 안내하십시오. (가) 수집 다음에 지원을

덕분에

+1

의 컬렉션 다음 쿼리보십시오, 당신은이 '사용자 ID'필드에 모음 다음에 질의하고 있지만 그 스키마에없는 것 같습니다. 질문에 전체 스키마를 표시하는 FollowsSchema가 있습니까? – shelman

+0

@shelman 제 잘못, 그것은 userID가 아닌 queryI입니다. –

+0

'select ('followingid -_id')'란 무엇인가요?'select ('followingid')' –

답변

1

당신은 초기 쿼리에서 다시 아무것도 못하고 있습니까? 문자열을 전달 중입니다

'500d18823e792d8814000001' 

을 profileId로 사용하지만 MongoDB는 문자열과 ObjectIds를 다르게 저장합니다. profileid 필드는 ObjectId이므로 쿼리를 실행하기 전에 문자열을 ObjectId로 변환해야합니다.

이 우선

'profileid':ObjectId('500d18823e792d8814000001')