Skip to content

Commit a3fd1f3

Browse files
committed
JAVA-1171: Allowing query documents to exceed the maximum document size by 16K, so that, for example, the findAndModify command can be used to upsert a 16M document. Without this headroom, the additional space taken by the surrounding command document would push it over 16M, and the driver would through an exception. 16K was chosen as the value because it matches the constant used in the server code base.
1 parent 439806c commit a3fd1f3

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/main/com/mongodb/DBCollectionImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646

4747
@SuppressWarnings("deprecation")
4848
class DBCollectionImpl extends DBCollection {
49+
50+
// Add an extra 16K to the maximum allowed size of a query document, so that, for example,
51+
// a 16M document can be upserted via findAndModify
52+
private static final int QUERY_DOCUMENT_HEADROOM = 16 * 1024;
53+
4954
private final DBApiLayer db;
5055
private final String namespace;
5156

@@ -73,7 +78,8 @@ QueryResultIterator find(DBObject ref, DBObject fields, int numToSkip, int batch
7378
trace("find: " + namespace + " " + JSON.serialize(ref));
7479
}
7580

76-
OutMessage query = OutMessage.query(this, options, numToSkip, chooseBatchSize(batchSize, limit, 0), ref, fields, readPref, encoder);
81+
OutMessage query = OutMessage.query(this, options, numToSkip, chooseBatchSize(batchSize, limit, 0), ref, fields, readPref,
82+
encoder, db.getMongo().getMaxBsonObjectSize() + QUERY_DOCUMENT_HEADROOM);
7783

7884
Response res = db.getConnector().call(_db, this, query, null, 2, readPref, decoder);
7985

src/test/com/mongodb/DBCollectionTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,15 @@ public void testFindAndRemoveTimeout() {
506506
}
507507
}
508508

509+
@Test
510+
public void testFindAndReplaceA16MDocument() {
511+
BasicDBObject documentWithJustId = new BasicDBObject("_id", 42);
512+
DBObject foundDocument = collection.findAndModify(documentWithJustId, new BasicDBObject("_id", 1), null, false,
513+
new BasicDBObject("_id", 42).append("b", new byte[16 * 1024 * 1024 - 30]), true,
514+
true);
515+
assertEquals(documentWithJustId, foundDocument);
516+
}
517+
509518
@Test
510519
public void testWriteConcernExceptionOnInsert() throws UnknownHostException {
511520
assumeTrue(isReplicaSet(getMongoClient()));

0 commit comments

Comments
 (0)