Skip to content

Commit 08a6766

Browse files
author
Justin Lee
committed
add remaining query modifier support
1 parent bb0b82f commit 08a6766

2 files changed

Lines changed: 197 additions & 1 deletion

File tree

src/main/com/mongodb/DBCursor.java

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,76 @@ public DBCursor( DBCollection collection , DBObject q , DBObject k, ReadPreferen
7777
_decoderFact = collection.getDBDecoderFactory();
7878
}
7979

80+
/**
81+
* Adds a comment to the query to identify queries in the database profiler output.
82+
*
83+
* @mongodb.driver.manual reference/operator/meta/comment/ $comment
84+
* @since 2.12
85+
*/
86+
public DBCursor comment(final String comment) {
87+
addSpecial(QueryOperators.COMMENT, comment);
88+
return this;
89+
}
90+
91+
/**
92+
* Limits the number of documents a cursor will return for a query.
93+
*
94+
* @mongodb.driver.manual reference/operator/meta/maxScan/ $maxScan
95+
* @see #limit(int)
96+
* @since 2.12
97+
*/
98+
public DBCursor maxScan(final int max) {
99+
addSpecial(QueryOperators.MAX_SCAN, max);
100+
return this;
101+
}
102+
103+
/**
104+
* Specifies a minimum exclusive upper limit for the index to use in a query.
105+
*
106+
* @mongodb.driver.manual reference/operator/meta/max/ $max
107+
* @since 2.12
108+
*/
109+
public DBCursor max(final DBObject max) {
110+
addSpecial(QueryOperators.MAX, max);
111+
return this;
112+
}
113+
114+
/**
115+
* Specifies a minimum inclusive lower limit for the index to use in a query.
116+
*
117+
* @mongodb.driver.manual reference/operator/meta/min/ $min
118+
* @since 2.12
119+
*/
120+
public DBCursor min(final DBObject min) {
121+
addSpecial(QueryOperators.MIN, min);
122+
return this;
123+
}
124+
125+
/**
126+
* Forces the cursor to only return fields included in the index.
127+
* @mongodb.driver.manual reference/operator/meta/returnKey/ $returnKey
128+
* @since 2.12
129+
*/
130+
public DBCursor returnKey() {
131+
addSpecial(QueryOperators.RETURN_KEY, true);
132+
return this;
133+
}
134+
135+
/**
136+
* Modifies the documents returned to include references to the on-disk location of each document. The location will be returned
137+
* in a property named {@code $diskLoc}
138+
* @mongodb.driver.manual reference/operator/meta/showDiskLoc/ $showDiskLoc
139+
* @since 2.12
140+
*/
141+
public DBCursor showDiskLoc() {
142+
addSpecial(QueryOperators.SHOW_DISK_LOC, true);
143+
return this;
144+
}
145+
80146
/**
81147
* Types of cursors: iterator or array.
82148
*/
83-
static enum CursorType { ITERATOR , ARRAY };
149+
static enum CursorType { ITERATOR , ARRAY }
84150

85151
/**
86152
* Creates a copy of an existing database cursor.

src/test/com/mongodb/DBCursorTest.java

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb;
1818

1919
import com.mongodb.util.TestCase;
20+
import org.junit.Assert;
2021
import org.junit.Test;
2122

2223
import java.net.UnknownHostException;
@@ -582,4 +583,133 @@ public void testMaxTimeForSize() {
582583
disableMaxTimeFailPoint();
583584
}
584585
}
586+
587+
private void insertData() {
588+
for (int i = 0; i < 10; i++) {
589+
collection.insert(new BasicDBObject("_id", i).append("x", i));
590+
}
591+
collection.ensureIndex("x");
592+
}
593+
594+
@Test
595+
public void testMaxScan() {
596+
insertData();
597+
countResults(new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
598+
.addSpecial("$maxScan", 4), 4);
599+
countResults(new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary()).maxScan(4), 4);
600+
}
601+
602+
@Test
603+
public void testMax() {
604+
insertData();
605+
countResults(new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
606+
.addSpecial("$max", new BasicDBObject("x", 4)), 4);
607+
countResults(new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
608+
.max(new BasicDBObject("x", 4)), 4);
609+
}
610+
611+
@Test
612+
public void testMin() {
613+
insertData();
614+
countResults(new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
615+
.addSpecial("$min", new BasicDBObject("x", 4)), 6);
616+
countResults(new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
617+
.min(new BasicDBObject("x", 4)), 6);
618+
}
619+
620+
@Test
621+
public void testReturnKey() {
622+
DBCursor cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
623+
.addSpecial("$returnKey", true);
624+
try {
625+
while (cursor.hasNext()) {
626+
Assert.assertNull(cursor.next()
627+
.get("_id"));
628+
}
629+
} finally {
630+
cursor.close();
631+
}
632+
cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
633+
.returnKey();
634+
try {
635+
while (cursor.hasNext()) {
636+
Assert.assertNull(cursor.next()
637+
.get("_id"));
638+
}
639+
} finally {
640+
cursor.close();
641+
}
642+
}
643+
644+
@Test
645+
public void testShowDiskLoc() {
646+
DBCursor cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
647+
.addSpecial("$showDiskLoc", true);
648+
try {
649+
while (cursor.hasNext()) {
650+
DBObject next = cursor.next();
651+
Assert.assertNotNull(next.toString(), next.get("$diskLoc"));
652+
}
653+
} finally {
654+
cursor.close();
655+
}
656+
cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
657+
.showDiskLoc();
658+
try {
659+
while (cursor.hasNext()) {
660+
DBObject next = cursor.next();
661+
Assert.assertNotNull(next.toString(), next.get("$diskLoc"));
662+
}
663+
} finally {
664+
cursor.close();
665+
}
666+
}
667+
668+
@Test(expected = MongoException.class)
669+
public void testSnapshotWithHint() {
670+
DBCursor cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
671+
.hint("x")
672+
.addSpecial("$snapshot", true);
673+
try {
674+
while (cursor.hasNext()) {
675+
DBObject next = cursor.next();
676+
}
677+
} finally {
678+
cursor.close();
679+
}
680+
}
681+
682+
@Test(expected = MongoException.class)
683+
public void testSnapshotWithSort() {
684+
DBCursor cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
685+
.sort(new BasicDBObject("x", 1))
686+
.addSpecial("$snapshot", true);
687+
try {
688+
while (cursor.hasNext()) {
689+
DBObject next = cursor.next();
690+
}
691+
} finally {
692+
cursor.close();
693+
}
694+
}
695+
696+
private void countResults(final DBCursor cursor, final int expected) {
697+
int count = 0;
698+
while (cursor.hasNext()) {
699+
cursor.next();
700+
count++;
701+
}
702+
cursor.close();
703+
assertEquals(expected, count);
704+
}
705+
706+
@Test
707+
public void testComment() {
708+
DBCursor cursor = new DBCursor(collection, new BasicDBObject(), new BasicDBObject(), ReadPreference.primary())
709+
.addSpecial("$comment", "test comment");
710+
while (cursor.hasNext()) {
711+
cursor.next();
712+
}
713+
}
714+
585715
}

0 commit comments

Comments
 (0)