<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:36:18 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-259] Cannot query a MongoDBRef</title>
                <link>https://jira.mongodb.org/browse/CSHARP-259</link>
                <project id="10041" key="CSHARP">C# Driver</project>
                    <description>&lt;p&gt;Question: &lt;a href=&quot;http://stackoverflow.com/questions/6512228/mongodbref-how-to-write-a-query&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;http://stackoverflow.com/questions/6512228/mongodbref-how-to-write-a-query&lt;/a&gt;&lt;br/&gt;
I&apos;m working with the MongoDB official driver (10Gen). And I cannot query a MonogoDBRef propertie. I have the following classes:&lt;/p&gt;

&lt;p&gt;    public class UserData()&lt;br/&gt;
    {&lt;br/&gt;
    private ObjectId id;&lt;br/&gt;
    public ObjectId _id&lt;br/&gt;
    {&lt;br/&gt;
        get &lt;/p&gt;
{ return id; }&lt;br/&gt;
        set { id = value; }&lt;br/&gt;
    }       &lt;br/&gt;
    &lt;span class=&quot;error&quot;&gt;&amp;#91;BsonElement(&amp;quot;Mail&amp;quot;)&amp;#93;&lt;/span&gt;&lt;br/&gt;
    public string Email { get; set; }&lt;br/&gt;
    public string Name{ get; set; }&lt;br/&gt;
    }&lt;br/&gt;
    public class UserSettings()&lt;br/&gt;
    {&lt;br/&gt;
    private ObjectId id;&lt;br/&gt;
    public ObjectId _id&lt;br/&gt;
    {&lt;br/&gt;
        get { return id; }
&lt;p&gt;        set &lt;/p&gt;
{ id = value; }
&lt;p&gt;    }       &lt;br/&gt;
    &lt;span class=&quot;error&quot;&gt;&amp;#91;BsonElement(&amp;quot;usr&amp;quot;)&amp;#93;&lt;/span&gt;&lt;br/&gt;
    public MongoDBRef User &lt;/p&gt;
{ get; set; }&lt;br/&gt;
    public List&amp;lt;SettingsUser&amp;gt; Settings{ get; set; }
&lt;p&gt;    }&lt;br/&gt;
I want to make a query that having the UserData I fetch the UserSettings of that user. I try the following but it does not work:&lt;/p&gt;

&lt;p&gt;var colletion = db.GetCollection&amp;lt;UserSettings&amp;gt;(&quot;UsrSettings&quot;); &lt;br/&gt;
collection.Find(Query.EQ(&quot;usr&quot;, usr._id);&lt;br/&gt;
also I try this:&lt;/p&gt;

&lt;p&gt; collection.Find(Query.EQ(&quot;usr&quot;, new MongoDBRef(&quot;UsrSettings&quot;, usr._id));&lt;br/&gt;
But it does not compile because MongoDBRef is not a BsonValue.&lt;/p&gt;

&lt;p&gt;Another try:&lt;/p&gt;

&lt;p&gt;  collection.FindOne(Query.EQ(&quot;usr.$id&quot;, User._id));&lt;br/&gt;
And I get the exception: Unexpected element &apos;$ref&apos;.&lt;/p&gt;
</description>
                <environment>Windows</environment>
        <key id="18824">CSHARP-259</key>
            <summary>Cannot query a MongoDBRef</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="robert@mongodb.com">Robert Stam</assignee>
                                    <reporter username="elranu">Mariano Vicario</reporter>
                        <labels>
                            <label>mongodbref</label>
                            <label>query,</label>
                    </labels>
                <created>Wed, 29 Jun 2011 13:49:37 +0000</created>
                <updated>Thu, 2 Apr 2015 18:28:01 +0000</updated>
                            <resolved>Sun, 3 Jul 2011 15:23:24 +0000</resolved>
                                                    <fixVersion>1.1</fixVersion>
                                                        <votes>0</votes>
                                    <watches>1</watches>
                                                                                                                <comments>
                            <comment id="40366" author="rstam" created="Sun, 3 Jul 2011 15:23:24 +0000"  >&lt;p&gt;Closed but will reopen if issue is not resolved to user&apos;s satisfaction.&lt;/p&gt;</comment>
                            <comment id="40365" author="rstam" created="Sun, 3 Jul 2011 15:22:14 +0000"  >&lt;p&gt;The first approach doesn&apos;t work because you are comparing an ObjectId to a MongoDBRef, which will never match.&lt;/p&gt;

&lt;p&gt;The second approach can work but you have to wrap the MongoDBRef like this:&lt;/p&gt;

&lt;p&gt;    var dbRef = new MongoDBRef(&quot;userdata&quot;, user1Data._id);&lt;br/&gt;
    var query1 = Query.EQ(&quot;usr&quot;, BsonDocumentWrapper.Create(dbRef));&lt;/p&gt;

&lt;p&gt;The third approach should have worked. I suspect you got the exception because you have some existing data that does not match your class declaration. Here&apos;s what my test data for the user settings collection looks like:&lt;/p&gt;

&lt;p&gt;&amp;gt; db.testusersettings.find()&lt;br/&gt;
{ &quot;_id&quot; : ObjectId(&quot;4e108809e447ad8e8cc69389&quot;), &quot;usr&quot; : &lt;/p&gt;
{ &quot;$ref&quot; : &quot;userdata&quot;, &quot;$id&quot; : ObjectId(&quot;4e108809e447ad8e8cc69388&quot;) }
&lt;p&gt;, &quot;Settings&quot; : &quot;Settings for John Doe&quot; }&lt;br/&gt;
{ &quot;_id&quot; : ObjectId(&quot;4e108809e447ad8e8cc6938b&quot;), &quot;usr&quot; : &lt;/p&gt;
{ &quot;$ref&quot; : &quot;userdata&quot;, &quot;$id&quot; : ObjectId(&quot;4e108809e447ad8e8cc6938a&quot;) }
&lt;p&gt;, &quot;Settings&quot; : &quot;Settings for Jane Doe&quot; }&lt;br/&gt;
&amp;gt;&lt;/p&gt;

&lt;p&gt;Note that I changed the type of your Settings property to string because I don&apos;t have the declaration of your SettingsUser class.&lt;/p&gt;

&lt;p&gt;Here&apos;s the complete test program you can play with:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.pastie.org/2158732&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;http://www.pastie.org/2158732&lt;/a&gt;&lt;/p&gt;
</comment>
                            <comment id="40362" author="rstam" created="Sun, 3 Jul 2011 15:03:13 +0000"  >&lt;p&gt;Can you display the matching document using the mongo shell? Just need to confirm that the existing document matches the class declaration.&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|hrh8rb:</customfieldvalue>

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