내 mongodb에 80,000 개의 문서가 있습니다. Java 드라이버를 사용하여 이러한 문서를 쿼리하고 있습니다. 특정 필드를 기반으로 내 문서를 정렬 한 다음이 결과에 고유 한 필터를 적용하려고했습니다. 정렬 옵션은 잘 작동하지만 별개의 문서는 얻을 수 없습니다. 나는 나의 작업 예제를 첨부했다.Java에서 Mongodb를 정렬하고 구별하십시오. 3.2
Document query = new Document("RetweetCount",-1);
MongoCursor<Document> cursor = collection.find().sort(query).iterator();
try{
while(cursor.hasNext()) {
Document dr = (Document) cursor.next();
String stat = dr.getString("status");
int retweetcount = dr.getInteger("RetweetCount");
//Sort works fine. I have to apply distinct here!!! distinct is based on status field
System.out.println(retweetcount+"--->"+stat);
}
}finally{
cursor.close();
}
}
http://api.mongodb.com/java/3.2/com/mongodb/operation/DistinctOperation.html 샘플 데이터베이스를 제공한다면 조금 알아낼 수 있습니다. – Newton
@Netwon 내 db 구조는 다음과 같습니다. {status : x, retweetcount : 2}, {status : y, retweetcount : 5}, {status : x, retweetcount : 3} 새로운 상태와 그 retweetcount와 함께 저장할 때마다. 상태에 대한 최고 retweetcount를 가져야하지만 내 db에 중복 상태가 있습니다. 상태를 얻고 싶습니다 : x와 retweetcount : 3, 그 상태에 대한 가장 높은 retweetcount 있습니다. – prabhu
컬렉션을 백업 할 수 있습니까? – Newton