Skip to content

Commit 50353fc

Browse files
committed
Helpers for listCollections and listIndexes now handle responses containing cursor documents
JAVA-1588
1 parent fe26cc0 commit 50353fc

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/main/com/mongodb/DBApiLayer.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,27 @@ public Set<String> getCollectionNames() {
149149
try {
150150
List<String> collectionNames = new ArrayList<String>();
151151
if (isServerVersionAtLeast(asList(2, 7, 7))) {
152-
CommandResult res = command(new BasicDBObject("listCollections", getName()), ReadPreference.primary());
152+
CommandResult res = command(new BasicDBObject("listCollections", getName()).append("cursor", new BasicDBObject()),
153+
ReadPreference.primary());
153154
if (!res.ok() && res.getCode() != 26) {
154155
res.throwOnError();
155156
} else {
156157
List<DBObject> collections = (List<DBObject>) res.get("collections");
157-
for (DBObject collectionInfo: collections) {
158-
collectionNames.add(collectionInfo.get("name").toString());
158+
if (collections != null) {
159+
for (DBObject collectionInfo : collections) {
160+
collectionNames.add(collectionInfo.get("name").toString());
161+
}
162+
} else {
163+
QueryResultIterator iterator = new QueryResultIterator(res, getMongo(), 0, DefaultDBDecoder.FACTORY.create(),
164+
res.getServerUsed());
165+
try {
166+
while (iterator.hasNext()) {
167+
DBObject collectionInfo = iterator.next();
168+
collectionNames.add(collectionInfo.get("name").toString());
169+
}
170+
} finally {
171+
iterator.close();
172+
}
159173
}
160174
}
161175
} else {

src/main/com/mongodb/DBCollectionImpl.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,28 @@ public List<DBObject> getIndexInfo() {
343343
List<DBObject> list = new ArrayList<DBObject>();
344344

345345
if (db.isServerVersionAtLeast(asList(2, 7, 6))) {
346-
CommandResult res = _db.command(new BasicDBObject("listIndexes", getName()), ReadPreference.primary());
346+
CommandResult res = _db.command(new BasicDBObject("listIndexes", getName()).append("cursor", new BasicDBObject()),
347+
ReadPreference.primary());
347348
if (!res.ok() && res.getCode() == 26) {
348349
return list;
349350
}
350351
res.throwOnError();
351-
for (DBObject indexDocument : (List<DBObject>) res.get("indexes")) {
352-
list.add(indexDocument);
352+
List<DBObject> indexes = (List<DBObject>) res.get("indexes");
353+
if (indexes != null) {
354+
for (DBObject indexDocument : indexes) {
355+
list.add(indexDocument);
356+
}
357+
} else {
358+
QueryResultIterator iterator = new QueryResultIterator(res, db.getMongo(), 0, DefaultDBDecoder.FACTORY.create(),
359+
res.getServerUsed());
360+
try {
361+
while (iterator.hasNext()) {
362+
DBObject collectionInfo = iterator.next();
363+
list.add(collectionInfo);
364+
}
365+
} finally {
366+
iterator.close();
367+
}
353368
}
354369
} else {
355370
BasicDBObject cmd = new BasicDBObject("ns", getFullName());

0 commit comments

Comments
 (0)