2016-08-31 2 views
0

다음 컬렉션이 MongoDB 3.2에 있습니다.두 배열에서 일치해야하는 몽고 쿼리

db.collection.insert(
    { 
     permissions: [ 
      {field: 'name', canEdit: true}, 
      {field: 'age', canEdit: false} 
      // ... lots of other fields 
     ], 
     inputs: [ 
      {field: 'name', edited: false}, 
      {field: 'age', edited: false} 
      // ... lots of other fields 
     ] 
    } 
); 

모두canEdit = trueedited = true 적어도 하나 개의 필드가 모든 문서를 반환하는 MongoDB의 쿼리를 작성하는 방법이 있나요? 의사의 방식으로 표현

위의 질문 :

find document D where exists 
    P in D.permissions 
    I in D.inputs 
    P.field = I.field and P.canEdit = true and I.edited = true 

답변

0

당신은 아마 collection 자체부터 $elemMatch 조건을 사용할 수 있습니다 내가 약간 수집 변형 예

db.collection_name.find(
    { collection: { $elemMatch: { "permissions.canEdit": true, "inputs.edited": true } } }) 
+0

같은 배열입니다. 제안한 쿼리는 필드에서 독립적으로 canEdit 및 편집 된 플래그를 찾습니다. 내가 확인해야 할 것은'P.field = I.field and P.canEdit = true and I.edited = true'입니다. –