<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:55:00 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[JAVA-1591] Setting DBCursor.batchSize() does not seem to work for certain queries.</title>
                <link>https://jira.mongodb.org/browse/JAVA-1591</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;Disclaimer:&lt;br/&gt;
I&apos;m not sure if this is a problem with the Java driver or a more general problem. It seems to reproduce with the mongo shell too. Feel free to re-assign as appropriate.&lt;/p&gt;

&lt;p&gt;Problem:&lt;br/&gt;
DBCursor&apos;s which are the result of a query which selects documents via a range query ($lt and $gt) and has a sort on the same field applied don&apos;t work correctly when DBCursor.batchSize() has been used before calling DBCursor.hasNext()/next() the first time. It may be a more general problem related to setting the batch size.&lt;/p&gt;

&lt;p&gt;I&apos;m attaching a reproducer for this issue. The expected output should be similar to the following:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Before insert count: 0 (if &amp;gt; 0 all items got removed)&lt;br/&gt;
Inserting data (2004 items total) ..................... done.&lt;br/&gt;
Reached count 2004&lt;br/&gt;
TestRecord461
&lt;br class=&quot;atl-forced-newline&quot; /&gt;[...]&lt;br/&gt;
TestRecord1537&lt;br/&gt;
Cursor actual numGetMore() == 10. Expected 10.&lt;br/&gt;
Actual cursor size was 1077&lt;br/&gt;
Query was: { &quot;$and&quot; : [ { &quot;timeStamp&quot; : { &quot;$gt&quot; : 1418211812266}} , { &quot;timeStamp&quot; : { &quot;$lt&quot; : 1418211835526}}]}&lt;br/&gt;
QUERIES done! Test SUCCESSFUL! Got &lt;b&gt;ALL&lt;/b&gt; 1077 results.&lt;br/&gt;
Test finished.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Actual output I get is:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Before insert count: 0 (if &amp;gt; 0 all items got removed)&lt;br/&gt;
Inserting data (2004 items total) ..................... done.&lt;br/&gt;
Reached count 2004&lt;br/&gt;
TestRecord461
&lt;br class=&quot;atl-forced-newline&quot; /&gt;[...]&lt;br/&gt;
TestRecord561&lt;br/&gt;
Cursor actual numGetMore() == 0. Expected 10.&lt;br/&gt;
Actual cursor size was 1077&lt;br/&gt;
Query was: { &quot;$and&quot; : [ { &quot;timeStamp&quot; : { &quot;$gt&quot; : 1418211812266}} , { &quot;timeStamp&quot; : { &quot;$lt&quot; : 1418211835526}}]}&lt;br/&gt;
QUERIES done! Test FAILED! Got only 100 results (out of 1077)&lt;br/&gt;
Test finished.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;If the tester.removeData() line gets commented out (i.e. keep data in the DB) one can reproduce the same results with the mongo shell. Code as follows:&lt;/p&gt;

&lt;p/&gt;
&lt;div id=&quot;syntaxplugin&quot; class=&quot;syntaxplugin&quot; style=&quot;border: 1px dashed #bbb; border-radius: 5px !important; overflow: auto; max-height: 30em;&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot; style=&quot;font-size: 1em; line-height: 1.4em !important; font-weight: normal; font-style: normal; color: black;&quot;&gt;
		&lt;tbody &gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;  margin-top: 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;var query = { &quot;$and&quot; : [ { &quot;timeStamp&quot; : { &quot;$gt&quot; : 1418211812266}} , { &quot;timeStamp&quot; : { &quot;$lt&quot; : 1418211835526}}]};&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;var myCursor = db[&quot;test-mongo-java&quot;].find(query).sort({&quot;timeStamp&quot;: 1})&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   margin-bottom: 10px;  width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;var count = 0; myCursor.batchSize(100).forEach(function(a) { print(a.name); count++; }); print(count)&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p/&gt;

&lt;p&gt;That forEach() call also only prints 100 elements while myCursor.size() prints &amp;gt; 100 (e.g. 1077).&lt;/p&gt;

&lt;p&gt;The Java reproducer can be run via (provided a mongod is running on local host on port 27518):&lt;/p&gt;
&lt;p/&gt;
&lt;div id=&quot;syntaxplugin&quot; class=&quot;syntaxplugin&quot; style=&quot;border: 1px dashed #bbb; border-radius: 5px !important; overflow: auto; max-height: 30em;&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot; style=&quot;font-size: 1em; line-height: 1.4em !important; font-weight: normal; font-style: normal; color: black;&quot;&gt;
		&lt;tbody &gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;  margin-top: 10px;   width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;javac -cp path/to/mongo-java-driver.jar TestMongo.java&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;   margin-bottom: 10px;  width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;java TestMongo&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p/&gt;</description>
                <environment>Linux, x86_64, Fedora 20&lt;br/&gt;
$ mongod --version&lt;br/&gt;
db version v2.4.6&lt;br/&gt;
Wed Dec 10 12:59:53.180 git version: nogitversion</environment>
        <key id="173705">JAVA-1591</key>
            <summary>Setting DBCursor.batchSize() does not seem to work for certain queries.</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="2">Won&apos;t Fix</resolution>
                                        <assignee username="ross@mongodb.com">Ross Lawley</assignee>
                                    <reporter username="jerboaa">Severin Gehwolf</reporter>
                        <labels>
                    </labels>
                <created>Wed, 10 Dec 2014 12:32:27 +0000</created>
                <updated>Wed, 10 Dec 2014 13:28:47 +0000</updated>
                            <resolved>Wed, 10 Dec 2014 13:28:47 +0000</resolved>
                                    <version>2.11.4</version>
                    <version>2.12.4</version>
                                                                        <votes>0</votes>
                                    <watches>2</watches>
                                                                                                                <comments>
                            <comment id="782616" author="ross@10gen.com" created="Wed, 10 Dec 2014 13:28:47 +0000"  >&lt;p&gt;Hi,&lt;/p&gt;

&lt;p&gt;This is not an issue with the Java driver but rather you are hitting a known bug with MongoDB 2.4 (&lt;a href=&quot;https://jira.mongodb.org/browse/SERVER-12438&quot; title=&quot;batch size with an unindexed sort in the new query system is inconsistent with the old behavior&quot; class=&quot;issue-link&quot; data-issue-key=&quot;SERVER-12438&quot;&gt;&lt;del&gt;SERVER-12438&lt;/del&gt;&lt;/a&gt;) which was fixed in MongoDB 2.6.  &lt;/p&gt;

&lt;p&gt;The bug was when using both &lt;tt&gt;batchSize&lt;/tt&gt; and &lt;tt&gt;sort&lt;/tt&gt; on a cursor where the &lt;tt&gt;sort&lt;/tt&gt; was on an unindexed field.  This caused the cursor to return fewer items than it should.  Barring an upgrade of MongoDB if you remove the sort or index the timestamp field or use the default batchSize and then it will work as expected.&lt;/p&gt;

&lt;p&gt;For future reference, a better place for questions regarding MongoDB usage or the Java driver specifics is the &lt;a href=&quot;https://groups.google.com/forum/#!forum/mongodb-user&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;mongodb-user mailinglist&lt;/a&gt; or &lt;a href=&quot;http://stackoverflow.com&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;stackoverflow&lt;/a&gt; as you will reach a boarder audience there and should get a faster response. This JAVA project is specifically for driver based bugs or issues.&lt;/p&gt;

&lt;p&gt;Hope that helps,&lt;/p&gt;

&lt;p&gt;Ross&lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                            <attachment id="59138" name="TestMongo.java" size="5062" author="jerboaa" created="Wed, 10 Dec 2014 12:32:27 +0000"/>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hs4nmn:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>152149</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>