2014-11-30 2 views
0

값 목록이 사전 목록의 사전 목록에있는 모든 레코드를 찾으려고합니다.MongoDB - dicts 목록의 목록에 있는지 확인하십시오.

"Tasks": [ 
    { 
     "description": "Buy weekly groceries", 
     "title": "Shopping", 
     "notes": "See if any offers for milk", 
     "completed": [ 
      "completed" 
     ], 
     "resources": [ 
      "246", 
      "444" 
     ] 
    }, 
    { 
     "description": "Fix car", 
     "title": "Mechanics", 
     "notes": "Change oil", 
     "completed": [ 
      "completed" 
     ], 
     "resources": [ 
      "2589", 
      "4818" 
     ] 
    } 

] 

나는 246이 같은 자원 '목록'의 값이며, 작업 목록에있는 모든 dicts를 반환하기 위해 노력하고있어 : 이것은 <pymongo.cursor.Cursor object at 0x030A10F0>을 반환

db.projects.find({ "Tasks.resources":{ '$in': ["246"] }}) 

는이 목록입니다 246이 자원 목록에있는 Tasks의 작업 대신에.

답변

1

find는 사용자에게 커서를 반환합니다. 개체를 원한다면 findOne()을 사용해보십시오. 다음 내용은 web mongodb을 참조하십시오. 코드가 예상대로 작동합니다.

> var a = {"Tasks": [ 
    { 
     "description": "Buy weekly groceries", 
     "title": "Shopping", 
     "notes": "See if any offers for milk", 
     "completed": [ 
      "completed" 
     ], 
     "resources": [ 
      "246", 
      "444" 
     ] 
    }, 
    { 
     "description": "Fix car", 
     "title": "Mechanics", 
     "notes": "Change oil", 
     "completed": [ 
      "completed" 
     ], 
     "resources": [ 
      "2589", 
      "4818" 
     ] 
    } 

]} 


> db.projects.insert(a); 
WriteResult({ "nInserted" : 1 }) 
> 
> db.projects.find({ "Tasks.resources":{ '$in': ["246"] }}) 
{ 
    "_id" : ObjectId("547b15e440694714925be65c"), 
    "Tasks" : [ 
     { 
      "notes" : "See if any offers for milk", 
      "title" : "Shopping", 
      "completed" : [ 
       "completed" 
      ], 
      "resources" : [ 
       "246", 
       "444" 
      ], 
      "description" : "Buy weekly groceries" 
     }, 
     { 
      "notes" : "Change oil", 
      "title" : "Mechanics", 
      "completed" : [ 
       "completed" 
      ], 
      "resources" : [ 
       "2589", 
       "4818" 
      ], 
      "description" : "Fix car" 
     } 
    ] 
} 

예를 들어 목록으로 변경하십시오.

def l = list(projects.find({ "Tasks.resources":{ '$in': ["246"] }})) 
print l 

또는

for task in projects.find({ "Tasks.resources":{ '$in': ["246"] }}): 
    print task 

pymongo tutorial를 참조하십시오.

+0

감사합니다. 모든 관련 문서를 반환하기 위해 'for 루프'가 필요하다는 것을 보여 주셨습니다. – user94628

관련 문제