정의
cursor.returnKey()중요
Mongo쉬 방법
This page documents a
mongoshmethod. This is not the documentation for a language-specific driver, such as Node.js.MongoDB API 드라이버의 경우 언어별 MongoDB 드라이버 설명서를 참조하세요.
팁
$metasupports the keyword"indexKey"to return index key metadata if an index is used. The use of{ $meta: "indexKey" }is preferred overcursor.returnKey().문서가 아닌 인덱스 키를 반환하도록 커서를 수정합니다.
The
cursor.returnKey()has the following form:cursor.returnKey() 반환합니다: The cursor that
returnKey()is attached to with a modified result set. This allows for additional cursor modifiers to be chained.
호환성
이 메서드는 다음 환경에서 호스팅되는 배포에서 사용할 수 있습니다.
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
참고
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise: MongoDB의 구독 기반 자체 관리 버전
MongoDB Community: MongoDB의 소스 사용 가능 무료 자체 관리 버전
행동
쿼리가 읽기 작업을 수행하는 데 인덱스를 사용하지 않으면 커서는 빈 문서를 반환합니다.
예시
restaurants collection에는 다음 스키마를 가진 문서가 포함되어 있습니다.
db.restaurants.insertOne( { _id: ObjectId("564f3a35b385149fc7e3fab9"), address: { building: "2780", coord: [ -73.98241999999999, 40.579505 ], street: "Stillwell Avenue", zipcode: "11224" }, borough: "Brooklyn", cuisine: "American ", grades: [ { date: ISODate("2014-06-10T00:00:00Z"), grade: "A", score: 5 }, { date: ISODate("2013-06-05T00:00:00Z"), grade: "A", score: 7 } ], name: "Riviera Caterer", restaurant_id: "40356018" } )
collection에는 기본 _id 인덱스 외에 두 개의 인덱스가 있습니다.
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "guidebook.restaurant" }, { "v" : 1, "key" : { "cuisine" : 1 }, "name" : "cuisine_1", "ns" : "guidebook.restaurant" }, { "v" : 1, "key" : { "_fts" : "text", "_ftsx" : 1 }, "name" : "name_text", "ns" : "guidebook.restaurant", "weights" : { "name" : 1 }, "default_language" : "english", "language_override" : "language", "textIndexVersion" : 3 }
The following code uses the cursor.returnKey() method to return only the indexed fields used for executing the query:
var csr = db.restaurant.find( { "cuisine" : "Japanese" } ) csr.returnKey()
그러면 다음이 반환됩니다:
{ "cuisine" : "Japanese" } { "cuisine" : "Japanese" } { "cuisine" : "Japanese" } { "cuisine" : "Japanese" } ...