<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:52:45 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-642] Using findAndModify to do a &quot;getOrCreate&quot;</title>
                <link>https://jira.mongodb.org/browse/JAVA-642</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;I would like to use findAndModify to get one document, without doing any modification on it. My need is to find a single document in a collection, or create it if it doesn&apos;t exist.&lt;/p&gt;

&lt;p&gt;Here&apos;s my java code (based on &lt;a href=&quot;http://api.mongodb.org/java/2.8.0/&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;http://api.mongodb.org/java/2.8.0/&lt;/a&gt;) and the error I get.&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;thead&gt;
			&lt;tr id=&quot;syntaxplugin_title&quot;&gt;
			&lt;td bgcolor=&quot;#f5f5f5&quot; style=&quot;font-family: Arial,sans-serif; color: #333; border-bottom: 1px solid #bbb; background-color: #f5f5f5 !important; font-weight: bold; line-height: 1em;&quot; &gt;
				&lt;p style=&quot;margin: 5px 10px; padding: 0;&quot;&gt;java&lt;/p&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/thead&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;MyDocument doc = (MyDocument) myCollection.findAndModify(query, null, null, false, null, true, true);&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;&amp;nbsp;&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;com.mongodb.CommandResult$CommandFailure: command failed [findandmodify]: { &quot;serverUsed&quot; : &quot;serverDns/ip:port&quot; , &quot;errmsg&quot; : &quot;need remove or update&quot; , &quot;ok&quot; : 0.0}&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;Now let&apos;s take a look at the parameters :&lt;br/&gt;
  query - query to match&lt;br/&gt;
  fields - fields to be returned&lt;br/&gt;
  sort - sort to apply before picking first document&lt;br/&gt;
  remove - if true, document found will be removed&lt;br/&gt;
  update - update to apply&lt;br/&gt;
  returnNew - if true, the updated document is returned, otherwise the old document is returned (or it would be lost forever)&lt;br/&gt;
  upsert - do upsert (insert if document not present) &lt;/p&gt;

&lt;p&gt;I have 2 servers that might access this database, so that&apos;s why I need an atomic-like &quot;getOrCreate&quot; behavior. There might be a better way of doing this.&lt;/p&gt;</description>
                <environment>Windows</environment>
        <key id="50460">JAVA-642</key>
            <summary>Using findAndModify to do a &quot;getOrCreate&quot;</summary>
                <type id="3" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14718&amp;avatarType=issuetype">Task</type>
                                            <priority id="4" iconUrl="https://jira.mongodb.org/images/icons/priorities/minor.svg">Minor - P4</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="mchampagne">Michael Champagne</reporter>
                        <labels>
                    </labels>
                <created>Thu, 13 Sep 2012 20:27:29 +0000</created>
                <updated>Wed, 11 Sep 2019 19:12:39 +0000</updated>
                            <resolved>Thu, 13 Sep 2012 21:15:15 +0000</resolved>
                                    <version>2.8.0</version>
                                                    <component>API</component>
                                        <votes>0</votes>
                                    <watches>2</watches>
                                                                                                                <comments>
                            <comment id="164528" author="mchampagne" created="Fri, 14 Sep 2012 16:38:26 +0000"  >&lt;p&gt;Didn&apos;t know that we can&apos;t update the _id field of a document.&lt;br/&gt;
Well, i will try this :&lt;/p&gt;
&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;have a &lt;b&gt;mongoId&lt;/b&gt; and &lt;b&gt;sqlId&lt;/b&gt; fields in my SQL table,&lt;/li&gt;
	&lt;li&gt;let mongo set the _id of my documents,&lt;/li&gt;
	&lt;li&gt;put that &lt;em&gt;mongoId&lt;/em&gt; in my sql table, as well as a &lt;em&gt;sqldId&lt;/em&gt; field,&lt;/li&gt;
	&lt;li&gt;use the &lt;em&gt;mongoId&lt;/em&gt; when &quot;joining&quot; mongo data with sql data,&lt;/li&gt;
	&lt;li&gt;use the &lt;em&gt;sqlId&lt;/em&gt; when joining in sql only.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;I know this isn&apos;t the better implementation, but it&apos;s temporary until I turn all sql tables into mongo collections. I&apos;m pretty sure it will work for my needs for now.&lt;/p&gt;</comment>
                            <comment id="164497" author="jeff.yemin" created="Fri, 14 Sep 2012 15:16:28 +0000"  >&lt;p&gt;I don&apos;t see a way around this.  The _id field is immutable once the document is created, and mongo will not generate _id&apos;s other than ObjectId&apos;s  Can you store the ObjectId as a binary field in your SQL database?&lt;/p&gt;</comment>
                            <comment id="164491" author="mchampagne" created="Fri, 14 Sep 2012 15:09:25 +0000"  >&lt;p&gt;I have another problem with findAndModify. I don&apos;t use the ID in my query, thus when the document isn&apos;t found, it is created with a mongo generated id. &lt;/p&gt;

&lt;p&gt;I don&apos;t want to use mongo generated ids because I need a numeric value (to put in the column of a SQL table).&lt;/p&gt;

&lt;p&gt;In my java application, I am generating an ID (which is a long) and I would like the document to have that ID when not found when using findAndModify. If I can&apos;t do that, I will have to do an update after findAndModify and that will be a problem when 2 servers will run that code at the same time. The first will create the document with a mongo id, then the other might get the document with that wrong id before the first server updates it.&lt;/p&gt;</comment>
                            <comment id="164440" author="jeff.yemin" created="Fri, 14 Sep 2012 12:27:28 +0000"  >&lt;p&gt;Yeah, it&apos;s not the most intuitive API.&lt;/p&gt;</comment>
                            <comment id="164435" author="mchampagne" created="Fri, 14 Sep 2012 12:10:15 +0000"  >&lt;p&gt;Thank you, it works!&lt;br/&gt;
I had tried &lt;em&gt;null&lt;/em&gt; and &lt;em&gt;new BasicDBObject()&lt;/em&gt; without success... I just didn&apos;t know how to make my command update nothing. An empty &lt;b&gt;$set&lt;/b&gt; command does just that  &lt;img class=&quot;emoticon&quot; src=&quot;https://jira.mongodb.org/images/icons/emoticons/smile.png&quot; height=&quot;16&quot; width=&quot;16&quot; align=&quot;absmiddle&quot; alt=&quot;&quot; border=&quot;0&quot;/&gt;&lt;/p&gt;</comment>
                            <comment id="164202" author="jeff.yemin" created="Thu, 13 Sep 2012 21:14:45 +0000"  >&lt;p&gt;It&apos;s because you&apos;re passing null to the update argument (3rd from the end).  Try something like:&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;MyDocument doc = (MyDocument) myCollection.findAndModify(query, null, null, false, new BasicDBObject(&quot;$set&quot;, new BasicDBObject()), true, true);&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&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|hrhngv:</customfieldvalue>

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