1
많은 필드가있는 User 컬렉션이 있다고 가정합니다. 예를 들어 [email protected]과 같은 사용자 메일을 알고 있습니다. 문어 프린트 출력에서 "email
"인 [email protected]
의 키를 출력하는 방법을 찾고 있습니다.mongo DB의 키 인쇄 키
{"email":"[email protected]"}
많은 필드가있는 User 컬렉션이 있다고 가정합니다. 예를 들어 [email protected]과 같은 사용자 메일을 알고 있습니다. 문어 프린트 출력에서 "email
"인 [email protected]
의 키를 출력하는 방법을 찾고 있습니다.mongo DB의 키 인쇄 키
{"email":"[email protected]"}
당신은 당신이에 관심이있는 문서, 등 그 만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);
}
})
덕분에 남자는 무엇을 나는 – Sam
감사를 찾고 있었다 힌트를위한 결함. 끝났어! – Sam