<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:42:38 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>[CSHARP-2466] InvalidOperationException with IQueryable and ExtraElements</title>
                <link>https://jira.mongodb.org/browse/CSHARP-2466</link>
                <project id="10041" key="CSHARP">C# Driver</project>
                    <description>&lt;p&gt;Trying to integrate Microsoft OData v4 stack with Mongo C# Driver. I would like to expose a Mongo collection and enable the OData $filter operation against the fields contained in the ExtraElements dictionary.&lt;/p&gt;

&lt;p&gt;I have created a minimal example of the issue. Beginning with an empty WebAPI project in Visual Studio 2015, I created the following model:&lt;/p&gt;

&lt;p&gt;public class TestEntity&lt;/p&gt;
 {
 [BsonId, BsonElement(&quot;_id&quot;)]
 public ObjectId _id \{ get; set; }

&lt;p&gt; public string Id { get; set; }&lt;/p&gt;

&lt;p&gt; &lt;span class=&quot;error&quot;&gt;&amp;#91;BsonExtraElements&amp;#93;&lt;/span&gt;&lt;br/&gt;
 public Dictionary&amp;lt;string, object&amp;gt; ExtraElements { get; set; }&lt;br/&gt;
 }&lt;/p&gt;

&lt;p&gt;I exposed EDM EntitySet via OData by adding the following at the end of `WebApiConfig.Register()` method:&lt;/p&gt;

&lt;p&gt;// OData&lt;br/&gt;
 var edmBuilder = new ODataConventionModelBuilder();&lt;br/&gt;
 edmBuilder.EntitySet&amp;lt;TestEntity&amp;gt;(&quot;test&quot;);&lt;/p&gt;

&lt;p&gt;config.MapODataServiceRoute(&quot;odata&quot;, &quot;odata&quot;, edmBuilder.GetEdmModel());&lt;/p&gt;

&lt;p&gt;Finally, I created a simple TestController:&lt;/p&gt;

&lt;p&gt;public class TestController : ODataController&lt;br/&gt;
 {&lt;br/&gt;
 &lt;span class=&quot;error&quot;&gt;&amp;#91;EnableQuery(AllowedFunctions = Microsoft.AspNet.OData.Query.AllowedFunctions.All)&amp;#93;&lt;/span&gt;&lt;br/&gt;
 public IQueryable&amp;lt;TestEntity&amp;gt; Get()&lt;/p&gt;
 {
 var mongo = new MongoClient();
 var db = mongo.GetDatabase(&quot;test&quot;);
 var collection = db.GetCollection&amp;lt;TestEntity&amp;gt;(&quot;test&quot;);
 var result = collection.AsQueryable() as IQueryable&amp;lt;TestEntity&amp;gt;;
 return result;
 }
&lt;p&gt; }&lt;/p&gt;

&lt;p&gt;I added an actual object to the Mongo collection:&lt;/p&gt;

&lt;p&gt;&amp;gt; db.test.insert( {Id:&apos;test&apos;, Name:&apos;testName&apos;}) WriteResult({ &quot;nInserted&quot; : 1 })&lt;br/&gt;
 &amp;gt; db.test.find()&lt;/p&gt;

&lt;p&gt;&amp;gt; { &quot;_id&quot; : ObjectId(&quot;5c266040fd7e5a3c63b33cd8&quot;), &quot;Id&quot; : &quot;test&quot;, &quot;Name&quot; : &quot;testName&quot; }&lt;/p&gt;


&lt;p&gt;Now I can query find as long as I don&apos;t try to use $filter on the ExtraElements (note that OData is not rendering the ObjectId field properly, but I do not believe this is related to the problem):&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://localhost:63927/odata/test&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;http://localhost:63927/odata/test&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;{&quot;@odata.context&quot;:&quot;http://localhost:63927/odata/$metadata#test&quot;,&quot;value&quot;:&lt;span class=&quot;error&quot;&gt;&amp;#91;\{&amp;quot;Id&amp;quot;:&amp;quot;test&amp;quot;,&amp;quot;Name&amp;quot;:&amp;quot;testName&amp;quot;,&amp;quot;_id&amp;quot;:{}}&amp;#93;&lt;/span&gt;}&lt;/p&gt;


&lt;p&gt;However, if I try to filter against the `Name` (one of the `ExtraElements`) the following error occurs:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://localhost:63927/odata/test?$filter=Name%20eq%20%27testName%27&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;http://localhost:63927/odata/test?$filter=Name%20eq%20%27testName%27&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;*Message: Convert(IIF((({document}&lt;/p&gt;
{ExtraElements} != null) AndAlso {document}{ExtraElements}
&lt;p&gt;.ContainsKey(\&quot;Name\&quot;)), {document}&lt;/p&gt;
{ExtraElements}
&lt;p&gt;.Item&lt;span class=&quot;error&quot;&gt;&amp;#91;\&amp;quot;Name\&amp;quot;&amp;#93;&lt;/span&gt;, null)) is not supported.*&lt;/p&gt;

&lt;p&gt; Exception: System.InvalidOperationException&lt;/p&gt;

&lt;p&gt; Stack Trace:&lt;br/&gt;
 at MongoDB.Driver.Linq.Translators.PredicateTranslator.GetFieldExpression(Expression expression)&lt;br/&gt;
 at MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateComparison(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression)&lt;br/&gt;
 at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node)&lt;br/&gt;
 at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry)&lt;br/&gt;
 at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateWhere(WhereExpression node)&lt;br/&gt;
 at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslatePipeline(PipelineExpression node)&lt;br/&gt;
 at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions)&lt;br/&gt;
 at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Execute(Expression expression)&lt;br/&gt;
 at MongoDB.Driver.Linq.MongoQueryableImpl`2.GetEnumerator()&lt;br/&gt;
 at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSetSerializer.WriteResourceSet(IEnumerable enumerable, IEdmTypeReference resourceSetType, ODataWriter writer, ODataSerializerContext writeContext)&lt;br/&gt;
 at Microsoft.AspNet.OData.Formatter.ODataOutputFormatterHelper.WriteToStream(Type type, Object value, IEdmModel model, ODataVersion version, Uri baseAddress, MediaTypeHeaderValue contentType, IWebApiUrlHelper internaUrlHelper, IWebApiRequestMessage internalRequest, IWebApiHeaders internalRequestHeaders, Func`2 getODataMessageWrapper, Func`2 getEdmTypeSerializer, Func`2 getODataPayloadSerializer, Func`1 getODataSerializerContext)&lt;br/&gt;
 at Microsoft.AspNet.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)&lt;br/&gt;
 &amp;#8212; End of stack trace from previous location where exception was thrown &amp;#8212;&lt;br/&gt;
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()&lt;br/&gt;
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)&lt;br/&gt;
 at System.Web.Http.WebHost.HttpControllerHandler.&amp;lt;WriteBufferedResponseContentAsync&amp;gt;d__1b.MoveNext()&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;</description>
                <environment></environment>
        <key id="662612">CSHARP-2466</key>
            <summary>InvalidOperationException with IQueryable and ExtraElements</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="9">Done</resolution>
                                        <assignee username="wan.bachtiar@mongodb.com">Wan Bachtiar</assignee>
                                    <reporter username="vugenti">Vincent Ugenti</reporter>
                        <labels>
                    </labels>
                <created>Fri, 28 Dec 2018 20:24:09 +0000</created>
                <updated>Thu, 25 Jun 2020 02:28:09 +0000</updated>
                            <resolved>Thu, 25 Jun 2020 02:28:09 +0000</resolved>
                                    <version>2.7.2</version>
                                                    <component>BSON</component>
                    <component>Linq</component>
                                        <votes>0</votes>
                                    <watches>1</watches>
                                                                                                                <comments>
                            <comment id="3223184" author="rachelle.palmer" created="Thu, 25 Jun 2020 02:28:09 +0000"  >&lt;p&gt;Hi there, thank you for reaching out to MongoDB. As this sounds more 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 forums, 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;Thank you!&lt;br/&gt;
Rachelle&lt;/p&gt;</comment>
                            <comment id="2101890" author="vugenti" created="Fri, 28 Dec 2018 20:28:21 +0000"  >&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;Also to clarify further, if the &quot;Name&quot; property is then added to the TestEntity class, the $filter operation works as expected.&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_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hu7qsn:</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>