2016-06-20 4 views
1

유성과 함께 MongoDB 데이터베이스에서 요소를 찾으려고합니다. 필자는 필자의 배열 구조를 걸러 내었지만 그 결과는 단일 요소이며 기준에 일치하는 모든 요소가 아닙니다.배열에 하나의 요소 만 반환되었습니다.

검색어 :

var json = Tests1VerlIR.find({}, {fields: {entries: {$elemMatch: {'payload.id': {$eq: this.params._id}} } } }).fetch(); 
this.response.setHeader('Content-Type', 'application/json'); 
this.response.end(JSON.stringify(json)); 

데이터 구조 :

{"entries": 
    [{"method":"POST", 
    "source":"ex", 
    "path":"/ex", 
    "time":1464615406900, 
    "payload":   
     {"slot_frame_number":"4", 
     "slot_HTUTemp":"2306", 
     "data":"0400f008561655270209a314", 
     "slot_BMEPres":"10069", 
     "slot_HTUHumi":"5283", 
     "slot_BMETemp":"2288", 
     "time":"1464615404", 
     "device":"79", 
     "slot_BMEHumi":"5718", 
     "signal":"7.22", 
     "id":"2"}, 
    "_id":"574c41ee578d01af3664cbaf"}, 
    {"method":"POST", 
    "source":"ex", 
    "path":"/ex", 
    "time":1464615406900, 
    "payload":   
     {"slot_frame_number":"4", 
     "slot_HTUTemp":"2306", 
     "data":"0400f008561655270209a314", 
     "slot_BMEPres":"10069", 
     "slot_HTUHumi":"5283", 
     "slot_BMETemp":"2288", 
     "time":"1464615404", 
     "device":"79", 
     "slot_BMEHumi":"5718", 
     "signal":"7.22", 
     "id":"2"}, 
    "_id":"574c41ee578d01af3664cbaf"}, {...}]} 

응답 :

[ 
    { 
     "_id": 
     { 
      "_str": "576155d7a605348159cd1f1a" 
     }, 
     "entries": 
     [ 
      { 
       "method": "POST", 
       "source": "ex", 
       "path": "/ex", 
       "time": 1464615406900, 
       "payload": 
       { 
        "slot_frame_number":"4", 
        "slot_HTUTemp":"2306", 
        "data":"0400f008561655270209a314", 
        "slot_BMEPres":"10069", 
        "slot_HTUHumi":"5283", 
        "slot_BMETemp":"2288", 
        "time":"1464615404", 
        "device":"79", 
        "slot_BMEHumi":"5718", 
        "signal":"7.22", 
        "id":"2" 
       }, 
       "_id": "574c41ee578d01af3664cbaf" 
      } 
     ] 
    } 
] 

답변

1

당신은 기본의 형태로 당신의 기준과 일치하는 배열의 여러 요소를 반환 할 수 없습니다. find() 쿼리. 둘 이상의 요소를 일치 시키려면 대신 .aggregate() 메소드를 사용해야합니다.

this link을 참조하십시오.

Tests1VerlIR.aggregate([ 
{ "$match": { "entries.payload.id": "2" } }, 

    // Unwind the array to denormalize 
    { "$unwind": "$entries" }, 

    // Match specific array elements 
    { "$match": { "entries.payload.id": "2" } }, 

    // Group back to array form 
    { "$group": { 
     "_id": "$_id", 
     "entries": { "$push": "$entries" } 
    }} 
]) 
-1

솔루션 :

var json = Tests1VerlIR.aggregate({"$unwind": "$entries"}, {$match: {'entries.payload.id': this.params._id} }); 
관련 문제