2017-10-23 5 views

답변

0

당신은 당신이에 관심이있는 문서, 등 그 email 속성이 반환되는 프로젝트를 찾은 다음 반환 된 문서에서 필드 이름을 잡을 수 있습니다. 예를 들어 :

db.collection.find(
     // find all documents (you'll probably have your own criteria to add here) 
     {}, 
     // project on the email attribute and - to reduce noise - exclude 
     // the default _id projection 
     {email: 1, '_id': 0} 
    ) 
    // it would make sense to limit here since you are only interested in an attribute name 
    // and, presumably, all relevant documents would have the same attribute name 
    .limit(1) 
    // since you only projected on email the returned document is 
    // expected to have a single attribute 
    .forEach(function(myDoc) { 
     for (var key in myDoc) { 
      // print the attribute name 
      print("key: " + key);  
     }  
    }) 

FWIW,이 ~입니다 드문 일이 아니다 적어도 당신이 이메일 속성에 관심이 알고 있다면 왜 당신은 당신의 손을 당신이 더 제공 아마도 경우 :)에를 얻기 위해 코드를 작성해야하기 때문에 에 대한 자세한 내용은을 원하십니까? 내 문서에서 '나에게 모든 키의 이름을 찾아'하는 일반적인 솔루션을 찾고 있다면

다음과 같은 일이 더 좋을 수 있습니다이었다

db.collection.find(
     // find all documents (you'll probably have your own criteria to add here) 
     {}, 
     // to reduce noise exclude the default _id projection 
     {'_id': 0} 
    ) 
    // it would make sense to limit here since you are only interested in an attribute name 
    // and, presumably, all relevant documents would have the same attribute name 
    .limit(1) 
    .forEach(function(myDoc) { 
     for (var key in myDoc) { 
      // print the attribute name 
      print("key: " + key);  
     }  
    }) 
+0

덕분에 남자는 무엇을 나는 – Sam

+0

감사를 찾고 있었다 힌트를위한 결함. 끝났어! – Sam