<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:39:56 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-1545] IdGenerator possibly not honoured on ReplaceOne upsert? (Works on InsertOne)</title>
                <link>https://jira.mongodb.org/browse/CSHARP-1545</link>
                <project id="10041" key="CSHARP">C# Driver</project>
                    <description>&lt;p&gt;Hopefully I&apos;m not misunderstanding something simple, but I have noticed I get different _id values generated in the database depending on whether I use InsertOne or ReplaceOne.  &lt;br/&gt;
I try to avoid any dependancy on MongoDB libraries etc, so the Id field in my objects is always a string. However, I want to use the ObjectId generation pattern. In much older versions of the driver, I was using the StringObjectIdGenerator, and am trying to do so now too.&lt;br/&gt;
I&apos;m using custom conventions to do my mapping (nothing strange in it, just that I use my own attributes to identify, for example, the ID field), and the important code looks like this:&lt;br/&gt;
                    var cmm = memberMap.ClassMap;&lt;br/&gt;
                    cmm.MapIdMember(memberMap.MemberInfo)&lt;br/&gt;
                        .SetSerializer(new StringSerializer(BsonType.ObjectId).WithRepresentation(BsonType.String))&lt;br/&gt;
                        .SetIgnoreIfDefault(true)&lt;br/&gt;
                        .SetIdGenerator(StringObjectIdGenerator.Instance);&lt;/p&gt;

&lt;p&gt;If I call InsertOne, I get this:&lt;br/&gt;
    &quot;_id&quot; : &quot;56a75e69e73e572dbc1b75c4&quot;&lt;/p&gt;

&lt;p&gt;and if I call ReplaceOne (with a new document/no id set) I get this:&lt;br/&gt;
    &quot;_id&quot; : ObjectId(&quot;56a75e6a87dc53ded31754e8&quot;)&lt;/p&gt;
</description>
                <environment></environment>
        <key id="260865">CSHARP-1545</key>
            <summary>IdGenerator possibly not honoured on ReplaceOne upsert? (Works on InsertOne)</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="-1">Unassigned</assignee>
                                    <reporter username="kevinza">Kevin Versfeld</reporter>
                        <labels>
                    </labels>
                <created>Tue, 26 Jan 2016 12:04:48 +0000</created>
                <updated>Wed, 27 Jan 2016 14:31:20 +0000</updated>
                            <resolved>Wed, 27 Jan 2016 14:31:20 +0000</resolved>
                                    <version>2.2</version>
                                                                        <votes>0</votes>
                                    <watches>1</watches>
                                                                                                                <comments>
                            <comment id="1154960" author="kevinza" created="Wed, 27 Jan 2016 09:53:52 +0000"  >&lt;p&gt;thanks for the comprehensive help, Craig. I think at this point, I&apos;m going to try the very last suggestion you make, which is avoiding using upserts (i.e. ReplaceOne). I think it should be ok, hopefully I haven&apos;t missed anything. Please go ahead and close this issue as &quot;By Design&quot;.&lt;/p&gt;</comment>
                            <comment id="1153874" author="craiggwilson" created="Tue, 26 Jan 2016 14:24:27 +0000"  >&lt;p&gt;1. To keep them as strings in your C# class, but stored as an ObjectId in the database, you&apos;d flip your BsonType designations. In the case of the StringSerializer, the default is BsonType.String. So, this is telling us to use a string in my classes, but reprsent it as an ObjectId in the database.&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;   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;.SetSerializer(new StringSerializer().WithRepresentation(BsonType.ObjectId))&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;2. Yeah, this is always a rough one. You can&apos;t update these documents because the &lt;tt&gt;_id&lt;/tt&gt; field is immutable. Hence, what you&apos;d actually be doing is deleting and re-inserting. There are a number of ways of doing this. What I would do is write a little C# console app that is repeatable.&lt;/p&gt;

&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;Make a backup.&lt;/li&gt;
	&lt;li&gt;use an IMongoCollection&amp;lt;BsonDocument&amp;gt;&lt;/li&gt;
	&lt;li&gt;iterates over every document in the collection whose _id &lt;a href=&quot;https://docs.mongodb.org/manual/reference/operator/query/type/&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;type is a string&lt;/a&gt; -&amp;gt; {_id: 
{ $type: 7 }
&lt;p&gt; },&lt;/p&gt;&lt;/li&gt;
	&lt;li&gt;Change the _id of the document by parsing the current _id into an ObjectId. -&amp;gt; doc&lt;span class=&quot;error&quot;&gt;&amp;#91;&amp;quot;_id&amp;quot;&amp;#93;&lt;/span&gt; = ObjectId.Parse((string)doc&lt;span class=&quot;error&quot;&gt;&amp;#91;&amp;quot;_id&amp;quot;&amp;#93;&lt;/span&gt;)&lt;/li&gt;
	&lt;li&gt;Insert the new document. (now we have the same document twice, but this is important so that we can ensure we haven&apos;t lost any data, missed any, etc...&lt;/li&gt;
	&lt;li&gt;Verify that we have exactly twice as many documents as we started with. (this only works if the system isn&apos;t still live, if it is, you may have to repeat this process).&lt;/li&gt;
	&lt;li&gt;Delete all the documents whose _ids are strings -&amp;gt; {_id: 
{ $type: 2 }
&lt;p&gt; },&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;There are 2 more alternatives. (a) just leave the old data. The .NET serializers will handle reading the data fine. However, it could cause issues with querying if you use Linq and query on the Id field. (b) Don&apos;t use ReplaceOne such that the only way data gets into your system is through InsertOne.&lt;/p&gt;

&lt;p&gt;Hope that helps.&lt;/p&gt;</comment>
                            <comment id="1153856" author="kevinza" created="Tue, 26 Jan 2016 14:10:16 +0000"  >&lt;p&gt;Oh, and: if I were to change to ObjectIds in the DB, er, is there a way to update existing data to change the format? (We have quite a bit...)&lt;/p&gt;</comment>
                            <comment id="1153855" author="kevinza" created="Tue, 26 Jan 2016 14:09:18 +0000"  >&lt;p&gt;Thanks for the response Craig, it makes a lot of sense actually. If you don&apos;t mind helping me out with a noob question, how would I change what I&apos;ve done to store ObjectIds, but keep them as strings in my code? Is it just the Serializer and Representation bits?&lt;/p&gt;</comment>
                            <comment id="1153849" author="craiggwilson" created="Tue, 26 Jan 2016 14:05:39 +0000"  >&lt;p&gt;If you want a string represented in the database, you are doing the right thing. However, ReplaceOne will never have identifiers generated client-side. This is simply because we have no idea whether or not there is a match for the filter and needs an identifier. So, the only methods that use an id generator are InsertOne and InsertMany.&lt;/p&gt;

&lt;p&gt;Thanks for the report and I wish we could do something, but there really isn&apos;t anything we can actually do. Depending on the trouble, it might make sense to store ObjectIds in the database instead of a string representation. They&apos;ll sort correctly and they&apos;ll take up a lot less space (12 bytes versus ~48 bytes) per document.&lt;/p&gt;

&lt;p&gt;Let me know if you have any other questions. Otherwise, I&apos;ll close this as Works as Designed.&lt;br/&gt;
Craig&lt;/p&gt;
</comment>
                            <comment id="1153787" author="kevinza" created="Tue, 26 Jan 2016 12:06:16 +0000"  >&lt;p&gt;Quick clarifications. &lt;br/&gt;
1) My class has a string Id property, and I would &lt;b&gt;prefer&lt;/b&gt; a string representation in the database too (purely because that&apos;s how all our current data is stored, and I don&apos;t want to have to change it.....)&lt;br/&gt;
2) &quot;memberMap&quot; in code above is the BsonMemberMap parameter of the Apply method in my custom IMemberMapConvention implementation.&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|hshq07:</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>