2016-06-13 2 views
1

이 질문은 바보 같은 경우 정말 미안, MongoDB를합니다 : 나는 다음과 같은 구조로 문서를 뽑아했습니다MongoDB : 클라이언트 측에서 DBRef를 해결하는 방법은 무엇입니까? 나는 새로운 해요

{ 
"_id" : ObjectId("575df70512a1aa0adbc2b496"), 
"name" : "something", 
"content" : { 
    "product" : { 
     "$ref" : "products", 
     "$id" : ObjectId("575ded1012a1aa0adbc2b394"), 
     "$db" : "mdb" 
    }, 
    "client" : { 
     "$ref" : "clients", 
     "$id" : ObjectId("575ded1012a1aa0adbc2b567"), 
     "$db" : "mdb" 
    } 
} 

나는 productsclients 컬렉션의 문서를 말하는 겁니다 곳. 클라이언트 측 (https://stackoverflow.com/a/4067227/1114975)에서 이러한 DBRef를 해결할 수 있음을 읽었습니다.

어떻게하면됩니까? 나는이 객체들을 질의하고 그것들을 문서에 끼워 넣는 것을 피하고 싶다. 감사합니다

답변

1

$lookup 연산자로 해결할 수 있습니다. 다음 통합 파이프 라인을 고려

// Execute aggregate, notice the pipeline is expressed as an Array 
collection.aggregate([ 
    { 
     "$lookup": { 
      "from": "product", 
      "localField": "content.product.$id", 
      "foreignField": "_id", 
      "as": "products" 
     } 
    }, 
    { 
     "$lookup": { 
      "from": "clients", 
      "localField": "content.client.$id", 
      "foreignField": "_id", 
      "as": "clients" 
     } 
    }, 
    ], function(err, result) { 
    console.log(result); 
}); 
+0

을 나는 $ 조회가 3.2 버전에서 작동합니다 생각합니다. 그렇지 않니? – harshavmb

+0

@harshavmb 수정 – chridam

1

나는이 문제와 기타 문제로 당신을 도와 Mongoose를 사용 고려할 것입니다. 이 경우 같은 것을 사용할 수 있습니다

Collection.find({}).populate('products').populate('clients') 
      .exec(function(err, books) {...}); 

Mongoose populate

관련 문제