2017-12-06 1 views
2
 db.absences.insert([ 
      { "_id" : 1, "student" : "Ann Aardvark", sickdays: [ new Date ("2018-05-01"),new Date ("2018-08-23") ] }, 
      { "_id" : 2, "student" : "Zoe Zebra", sickdays: [ new Date ("2018-02-01"),new Date ("2018-05-23") ] }, 
     ]) 

    db.holidays.insert([ 
     { "_id" : 1, year: 2018, name: "New Years", date: new Date("2018-01-01") }, 
     { "_id" : 2, year: 2018, name: "Pi Day", date: new Date("2018-03-14") }, 
     { "_id" : 3, year: 2018, name: "Ice Cream Day", date: new Date("2018-07-15") }, 
     { "_id" : 4, year: 2017, name: "New Years", date: new Date("2017-01-01") }, 
     { "_id" : 5, year: 2017, name: "Ice Cream Day", date: new Date("2017-07-16") } 
    ]) 

db.absences.aggregate([ 
    { 
     $lookup: 
     { 
      from: "holidays", 
      pipeline: [ 
       { $match: { year: 2018 } }, 
       { $project: { _id: 0, date: { name: "$name", date: "$date" } } }, 
       { $replaceRoot: { newRoot: "$date" } } 
      ], 
      as: "holidays" 
     } 
    } 
]) 

집계 쿼리를 조회 할 때 파이프 라인을 사용하려고합니다. Mongodb 문서에서와 동일하게 오류가 발생합니다.

Unable to execute the selected commands 

Mongo Server error (MongoCommandException): Command failed with error 4570: 'arguments to $lookup must be strings, pipeline: [ { $match: { year: 2018.0 } }, { $project: { _id: 0.0, date: { name: "$name", date: "$date" } } }, { $replaceRoot: { newRoot: "$date" } } ] is type array' on server localhost:27017. 

The full response is: 
{ 
    "ok" : 0.0, 
    "errmsg" : "arguments to $lookup must be strings, pipeline: [ { $match: { year: 2018.0 } }, { $project: { _id: 0.0, date: { name: \"$name\", date: \"$date\" } } }, { $replaceRoot: { newRoot: \"$date\" } } ] is type array", 
    "code" : NumberInt(4570), 
    "codeName" : "Location4570" 
} 

저는 mongodb v3.4를 사용하고 있습니다.

답변

3

당신은 MongoDB를의 V3.4에 MongoDB를의 V3.6에서 $lookup 기능 (구문)를 사용하려고하기 때문에

MongoDB를의 V3.4 $lookup 구문 :

{ 
    $lookup: 
    { 
     from: <collection to join>, 
     localField: <field from the input documents>, 
     foreignField: <field from the documents of the "from" collection>, 
     as: <output array field> 
    } 
} 

MongoDB v3.6 $lookup 구문 :

{ 
    $lookup: 
    { 
     from: <collection to join>, 
     let: { <var_1>: <expression>, …, <var_n>: <expression> }, 
     pipeline: [ <pipeline to execute on the collection to join> ], 
     as: <output array field> 
    } 
} 

https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

+0

본인은이 설명서를 참조하여 시험해 보았습니다. 본인의 질문에서도 언급했습니다. 귀하의 컴퓨터에서 사용해보십시오. 그것은 당신에게 같은 오류를 줄 것이다. – Nakarmi

-1

은 $ 조회에서 당신은 나중에 조회 후 등등 collectionfirst과 경기, 프로젝트를하고 가입 할 수있다. 그리고 휴일 수집을 말하는 외계인을 가져야합니다.

등 :

{ 
    $lookup: 
    { 
     from: "holidays", 
     localField: "holidaysID", //yourLocalfield, it is local field in absences. you should have it to join 2 collections 
     foreignField : "_id", //theforeignfield, it is _id in the holidays 
     as: "holidays" 
    } 
},{ $match: { year: 2018 } }, 
{ $project: { _id: 0, date: { name: "$name", date: "$date" } } }, 
{ $replaceRoot: { newRoot: "$date" } } 

는 도움이되기를 바랍니다.

+0

MongoDB 버전에 따라 다릅니다. 문서를 살펴보십시오 https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#join-conditions-and-uncorrelated-sub-queries (-; – Neodan

+0

답변 해 주셔서 감사합니다. 그러나 이것은 아닙니다. 내 문제를 해결할 수 없습니다. $ lookup은 공휴일의 모든 문서를 가져 오는 반면 문서에서 선택된 데이터 만 가져와야합니다. – Nakarmi