2013-06-19 4 views
1

Java를 사용하여 데이터베이스에서 mongo "_ids"목록을 가져 오려고합니다. 데이터베이스의 다른 부분은 필요 없으며 "_id"만 있습니다. ...mongo 데이터베이스에서 _ids 검색

// Another method queries for all objects of a certain type within the database. 
Collection<MyObject> thingies = this.getMyObjects(); 

Collection<String> ids = new LinkedList<String>(); 
for (MyObject thingy : thingies) { 
    ids.add(thingy.getGuid()); 
} 

이 비록 끔찍하게 비효율적 인 것처럼 보인다 그들의 "_ids"특정 유형의 개체에 대한 몽고를 조회하고 반환하는 방법이있다 :

내가 지금 뭘하는지입니다 전체 객체를 재조합하고 추출하지 않고도?

감사합니다.

+0

[자바 MongoDB의에서 필드의 하위 집합을 가져]의 중복 가능성 (http://stackoverflow.com/questions/10714939/retrieving-a-subset-of -fields-mongodb-in-java) – WiredPrairie

답변

1

find() 메서드에는 쿼리에서 검색 할 키 또는 원하지 않는 키를 전달할 수있는 오버로드가 있습니다.

그래서 당신이 시도 할 수 :

BasicDBObject qyery = new BasicDBObject("someKey","someValue"); 
BasicDBObject keys = new BasicDBObject("_id", 1); 
DBCursor cursor = collection.find(query, keys);