<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 09:03:32 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-5019] Java driver 4.2.3 Document.getList throws ClassCastException</title>
                <link>https://jira.mongodb.org/browse/JAVA-5019</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;while using java driver 4.2.3 with server version - 5.0.12&lt;/p&gt;

&lt;p&gt;Document class getList method throws ClassCastExcepion below are the details.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Document in DB:&lt;/b&gt;&lt;/p&gt;
&lt;h4&gt;&lt;a name=&quot;&quot;&gt;&lt;/a&gt;&lt;font color=&quot;#505f79&quot;&gt;&lt;em&gt;&lt;sub&gt;{&lt;/sub&gt;&lt;/em&gt;&lt;/font&gt;&lt;/h4&gt;
&lt;p&gt;&lt;font color=&quot;#505f79&quot;&gt;&#160; &lt;em&gt;&lt;sub&gt;&#8220;name&#8221;: &#8220;hr&#8221;,&lt;/sub&gt;&lt;/em&gt;&lt;/font&gt;&lt;br/&gt;
&lt;font color=&quot;#505f79&quot;&gt;&#160; &lt;em&gt;&lt;sub&gt;&#8220;id&#8221;: 1,&lt;/sub&gt;&lt;/em&gt;&lt;/font&gt;&lt;br/&gt;
&lt;font color=&quot;#505f79&quot;&gt;&#160; &lt;em&gt;&lt;sub&gt;&#8220;employees&#8221;: [&lt;/sub&gt;&lt;/em&gt;&lt;/font&gt;&lt;br/&gt;
&lt;font color=&quot;#505f79&quot;&gt;&#160; &#160; &#160;&lt;em&gt;&lt;sub&gt;{&lt;/sub&gt;&lt;/em&gt;&lt;/font&gt;&lt;br/&gt;
&lt;font color=&quot;#505f79&quot;&gt;&#160; &#160; &#160; &#160;&lt;em&gt;&lt;sub&gt;&#8220;name&#8221;: &#8220;paul&#8221;,&lt;/sub&gt;&lt;/em&gt;&lt;/font&gt;&lt;br/&gt;
&lt;font color=&quot;#505f79&quot;&gt;&#160; &#160; &#160; &#160;&lt;em&gt;&lt;sub&gt;&#8220;empId&#8221;: 1&lt;/sub&gt;&lt;/em&gt;&lt;/font&gt;&lt;br/&gt;
&lt;font color=&quot;#505f79&quot;&gt;&#160; &#160; &#160;&lt;em&gt;&lt;sub&gt;},&lt;/sub&gt;&lt;/em&gt;&lt;/font&gt;&lt;br/&gt;
&lt;font color=&quot;#505f79&quot;&gt;&#160; &#160; &lt;em&gt;&lt;sub&gt;{&lt;/sub&gt;&lt;/em&gt;&lt;/font&gt;&lt;br/&gt;
&lt;font color=&quot;#505f79&quot;&gt;&#160; &#160; &#160; &lt;em&gt;&lt;sub&gt;&#8220;name&#8221;: &#8220;nick&#8221;,&lt;/sub&gt;&lt;/em&gt;&lt;/font&gt;&lt;br/&gt;
&lt;font color=&quot;#505f79&quot;&gt;&#160; &#160; &#160; &lt;em&gt;&lt;sub&gt;&#8220;empId&#8221;: 2&lt;/sub&gt;&lt;/em&gt;&lt;/font&gt;&lt;br/&gt;
&lt;font color=&quot;#505f79&quot;&gt;&#160; &#160; &lt;em&gt;&lt;sub&gt;}&lt;/sub&gt;&lt;/em&gt;&lt;/font&gt;&lt;br/&gt;
&lt;font color=&quot;#505f79&quot;&gt;&#160; &#160;&lt;em&gt;&lt;sub&gt;]&lt;/sub&gt;&lt;/em&gt;&lt;/font&gt;&lt;br/&gt;
&lt;font color=&quot;#505f79&quot;&gt;&lt;em&gt;&lt;sub&gt;}&lt;/sub&gt;&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;
&lt;h2&gt;&lt;a name=&quot;&quot;&gt;&lt;/a&gt;&lt;b&gt;&lt;font color=&quot;#505f79&quot;&gt;&lt;sub&gt;Code:&lt;/sub&gt;&lt;/font&gt;&lt;/b&gt;&lt;/h2&gt;

&lt;p&gt;public void getEmpList(){&lt;br/&gt;
&#160; &#160; &#160; &#160; MongoCollection&amp;lt;Document&amp;gt; coll = mongoDatabase.getCollection(&quot;Department&quot;);&lt;br/&gt;
&#160; &#160; &#160; &#160; try &lt;/p&gt;
{
&#160; &#160; &#160; &#160; &#160; &#160; Document doc = coll.find().first();
&#160; &#160; &#160; &#160; &#160; &#160; List&amp;lt;Employee&amp;gt; employees = doc.getList(&quot;employees&quot;, Employee.class);
&#160; &#160; &#160; &#160; }
&lt;p&gt;catch(Exception e)&lt;/p&gt;
{
&#160; &#160; &#160; &#160; &#160; &#160; e.printStackTrace();
&#160; &#160; &#160; &#160; }
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;public class Employee{&lt;/p&gt;

&lt;p&gt;&#160; &#160; private String name;&lt;br/&gt;
&#160; &#160; private Integer empId;&lt;/p&gt;

&lt;p&gt;&#160; &#160; &#160;public String getName() &lt;/p&gt;
{
&#160; &#160; &#160; &#160; &#160;return name;
&#160; &#160; }

&lt;p&gt;&#160; &#160; public void setName(String name) &lt;/p&gt;
{
&#160; &#160; &#160; &#160;this.name = name;
&#160; &#160; }

&lt;p&gt;&#160; &#160;public Integer getEmpId() &lt;/p&gt;
{
&#160; &#160; &#160; &#160;return empId;
&#160; &#160;}

&lt;p&gt;&#160; &#160; public void setEmpId(Integer empId) &lt;/p&gt;
{
&#160; &#160; &#160; &#160;this.empId = empId;
&#160; &#160; }
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;java.lang.ClassCastException: List element cannot be cast to com.Employee&lt;/em&gt;&lt;br/&gt;
&lt;em&gt;at org.bson.Document.constructValuesList(Document.java:383)&lt;/em&gt;&lt;br/&gt;
&lt;em&gt;at org.bson.Document.getList(Document.java:350)&lt;/em&gt;&lt;/p&gt;</description>
                <environment></environment>
        <key id="2360850">JAVA-5019</key>
            <summary>Java driver 4.2.3 Document.getList throws ClassCastException</summary>
                <type id="6" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14720&amp;avatarType=issuetype">Question</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="13202">Works as Designed</resolution>
                                        <assignee username="-1">Unassigned</assignee>
                                    <reporter username="s.nivas123@gmail.com">Sreenivas Gurramkonda</reporter>
                        <labels>
                    </labels>
                <created>Wed, 7 Jun 2023 07:13:02 +0000</created>
                <updated>Fri, 27 Oct 2023 13:20:50 +0000</updated>
                            <resolved>Wed, 7 Jun 2023 08:17:29 +0000</resolved>
                                                                                        <votes>0</votes>
                                    <watches>3</watches>
                                                                                                                <comments>
                            <comment id="5481269" author="s.nivas123@gmail.com" created="Wed, 7 Jun 2023 14:21:06 +0000"  >&lt;p&gt;Thank you &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=ross%40mongodb.com&quot; class=&quot;user-hover&quot; rel=&quot;ross@mongodb.com&quot;&gt;ross@mongodb.com&lt;/a&gt; for the information. I tested it and it threw exception while iteration.&lt;/p&gt;</comment>
                            <comment id="5480966" author="ross@10gen.com" created="Wed, 7 Jun 2023 13:47:33 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=s.nivas123%40gmail.com&quot; class=&quot;user-hover&quot; rel=&quot;s.nivas123@gmail.com&quot;&gt;s.nivas123@gmail.com&lt;/a&gt;,&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt; the below code somehow works without any classcast exception:&lt;/p&gt;

&lt;p&gt;List&amp;lt;Employee&amp;gt; employees = (List&amp;lt;Employee&amp;gt;) doc.get(&quot;employees&quot;);&lt;/p&gt;

&lt;p&gt;can you pls give some insights on this&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Correct but once you start iterating the employees list it will throw an exception - its how Java handles casting.&lt;/p&gt;</comment>
                            <comment id="5480298" author="s.nivas123@gmail.com" created="Wed, 7 Jun 2023 08:54:29 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=ross%40mongodb.com&quot; class=&quot;user-hover&quot; rel=&quot;ross@mongodb.com&quot;&gt;ross@mongodb.com&lt;/a&gt; thanks for your response. So it looks like I have to map the full Document to POJOs for it to work, but I have one question, the below code somehow works without any classcast exception&lt;/p&gt;

&lt;p&gt;List&amp;lt;Employee&amp;gt; employees = (List&amp;lt;Employee&amp;gt;) doc.get(&quot;employees&quot;);&lt;/p&gt;

&lt;p&gt;can you pls give some insights on this&lt;/p&gt;</comment>
                            <comment id="5480255" author="ross@10gen.com" created="Wed, 7 Jun 2023 08:17:29 +0000"  >&lt;p&gt;I&apos;m closing this ticket as it is the expected behaviour.&lt;/p&gt;

&lt;p&gt;Ross&lt;/p&gt;</comment>
                            <comment id="5480253" author="ross@10gen.com" created="Wed, 7 Jun 2023 08:16:23 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=s.nivas123%40gmail.com&quot; class=&quot;user-hover&quot; rel=&quot;s.nivas123@gmail.com&quot;&gt;s.nivas123@gmail.com&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;Thank you for reaching out. The error message describes the issue, but in simple terms you are trying to cast a &lt;tt&gt;List&amp;lt;Document&amp;gt;&lt;/tt&gt; into a &lt;tt&gt;List&amp;lt;Employee&amp;gt;&lt;/tt&gt; hence the exception. If you want to use POJOs you should check out the &lt;a href=&quot;https://www.mongodb.com/docs/drivers/java/sync/v4.9/fundamentals/data-formats/document-data-format-pojo/&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;POJO documentation&lt;/a&gt;. Your end solution should be modelled using something like: &lt;tt&gt;MongoCollection&amp;lt;Department&amp;gt;&lt;/tt&gt; - where a &lt;tt&gt;Department&lt;/tt&gt; Pojo represents the full document in the DB.&lt;/p&gt;

&lt;p&gt;For future reference as this sounds like a support issue, I wanted to give you some resources to get this question answered more quickly:&lt;/p&gt;
&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;Our MongoDB support portal, located at &lt;a href=&quot;https://support.mongodb.com/welcome&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;support.mongodb.com&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;Our MongoDB community portal, located &lt;a href=&quot;https://developer.mongodb.com/community/forums/&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;here&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;If you are an Atlas customer, there is free support offered 24/7 in the lower right hand corner of the UI&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;All the best,&lt;/p&gt;

&lt;p&gt;Ross Lawley&lt;/p&gt;</comment>
                            <comment id="5480184" author="dbeng-pm-bot" created="Wed, 7 Jun 2023 07:13:05 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=s.nivas123%40gmail.com&quot; class=&quot;user-hover&quot; rel=&quot;s.nivas123@gmail.com&quot;&gt;s.nivas123@gmail.com&lt;/a&gt;, thank you for reporting this issue! The team will look into it and get back to you soon. &lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                    </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_14266" key="com.atlassian.jira.plugin.system.customfieldtypes:textarea">
                        <customfieldname>Documentation Changes Summary</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>&lt;p&gt;1.  What would you like to communicate to the user about this feature?&lt;br/&gt;
2.  Would you like the user to see examples of the syntax and/or executable code and its output?&lt;br/&gt;
3.  Which versions of the driver/connector does this apply to?&lt;/p&gt;</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|i1utu0:</customfieldvalue>

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