MongoDB에서 커서 값을 얻으려면 hasNext()를 사용하십시오. 문서로 컬렉션을 만들자 −
> db.demo191.insertOne({"EmployeeId":1,"EmployeeName":"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad95303d395bdc21346c5") } > db.demo191.insertOne({"EmployeeId":2,"EmployeeName":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad95f03d395bdc21346c6") } > db.demo191.insertOne({"EmployeeId":1,"EmployeeName":"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad96803d395bdc21346c7") } > db.demo191.insertOne({"EmployeeId":1,"EmployeeName":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad97003d395bdc21346c8") }
find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo191.find();
이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5e3ad95303d395bdc21346c5"), "EmployeeId" : 1, "EmployeeName" : "Chris Brown" } { "_id" : ObjectId("5e3ad95f03d395bdc21346c6"), "EmployeeId" : 2, "EmployeeName" : "David Miller" } { "_id" : ObjectId("5e3ad96803d395bdc21346c7"), "EmployeeId" : 1, "EmployeeName" : "John Doe" } { "_id" : ObjectId("5e3ad97003d395bdc21346c8"), "EmployeeId" : 1, "EmployeeName" : "John Smith" }
다음은 커서 개체의 값을 가져오는 쿼리입니다 -
> var cursor = db.demo191.find( { "EmployeeId":1 } ); > while (cursor.hasNext()) { ... print(tojson(cursor.next())); ...}
이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5e3ad95303d395bdc21346c5"), "EmployeeId" : 1, "EmployeeName" : "Chris Brown" } { "_id" : ObjectId("5e3ad96803d395bdc21346c7"), "EmployeeId" : 1, "EmployeeName" : "John Doe" } { "_id" : ObjectId("5e3ad97003d395bdc21346c8"), "EmployeeId" : 1, "EmployeeName" : "John Smith" }