Message from discussion
Upsert Array Issue
Received: by 10.58.69.116 with SMTP id d20mr5134047veu.15.1351249882497;
Fri, 26 Oct 2012 04:11:22 -0700 (PDT)
X-BeenThere: mongodb-csharp@googlegroups.com
Received: by 10.52.141.116 with SMTP id rn20ls4537689vdb.5.gmail; Fri, 26 Oct
2012 04:11:20 -0700 (PDT)
Received: by 10.52.66.235 with SMTP id i11mr4220175vdt.7.1351249879684;
Fri, 26 Oct 2012 04:11:19 -0700 (PDT)
Date: Fri, 26 Oct 2012 04:11:19 -0700 (PDT)
From: Neal Kranes <nkra...@gmail.com>
To: mongodb-csharp@googlegroups.com
Message-Id: <1b4c4861-cba3-4433-aa06-254582b941e5@googlegroups.com>
In-Reply-To: <CAJCJWG05vby0ovNQwpXNF_KcxwBhgbOX3qDymvQQ2uJUtbVfgw@mail.gmail.com>
References: <e01807f7-6c83-42c5-8d1c-4ef07cfcc24c@googlegroups.com>
<222250db-5cec-49bc-ba59-2826d4b6bd32@googlegroups.com>
<CAJCJWG05vby0ovNQwpXNF_KcxwBhgbOX3qDymvQQ2uJUtbVfgw@mail.gmail.com>
Subject: Re: [mongodb-csharp] Re: Upsert Array Issue
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_571_23511527.1351249879141"
------=_Part_571_23511527.1351249879141
Content-Type: multipart/alternative;
boundary="----=_Part_572_21250594.1351249879141"
------=_Part_572_21250594.1351249879141
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
I figured. Is there any way to Upsert the array element purely based on
_id? Is the way I've outlined pretty much the best method to perform the
Upsert at this time? Is there a better method?
Thanks!
Neal
On Thursday, October 25, 2012 10:45:05 PM UTC-4, Robert Stam wrote:
>
> Your cachedate values are different, and that's enough for $addToSet to
> consider it a new element of the unifiedassociations array.
>
> On Thu, Oct 25, 2012 at 5:06 PM, Neal Kranes <nkr...@gmail.com<javascript:>
> > wrote:
>
>> I ended up putting a 2 phase Try Update / Else Insert logic:
>> // Try to update item first
>> var query = MongoDB.Driver.Builders.Query.And(
>> MongoDB.Driver.Builders.Query.EQ("_id", "F_S_17460317_0"),
>> MongoDB.Driver.Builders.Query.EQ("unifiedassociations._id",
>> association._id)
>> );
>> var update = Update.Set("unifiedassociations", "[document, removed for
>> brevity]");
>> var updateAttemptResult = _featureCollection.Update(query, update,
>> SafeMode.True);
>>
>> // If not, insert
>> if (updateAttemptResult.DocumentsAffected == 0)
>> {
>> query = MongoDB.Driver.Builders.Query.EQ("_id",
>> GetFeatureCacheKey(feature));
>> update = Update.AddToSet("unifiedassociations", "[document, removed
>> for brevity]");
>> updateAttemptResult = _featureCollection.Update(query, update,
>> SafeMode.True);
>> }
>>
>> Is this my best bet?
>>
>> I was hoping to get it done in 1 statement, but after reading a bit it
>> seems that might not be possible.
>>
>> Neal
>>
>> On Thursday, October 25, 2012 11:38:10 AM UTC-4, Neal Kranes wrote:
>>>
>>> I have a document that I want to Upsert an array into. Here's the
>>> initial document:
>>> {
>>> "_id" : "F_S_17460317_0",
>>> "featureid" : 17460317,
>>> "featuretype" : "S",
>>> "builddate" : ISODate("2012-10-16T13:28:00Z"**),
>>> "lastediteddate" : ISODate("2012-05-31T07:23:37.**963Z")
>>> }
>>>
>>> var query = MongoDB.Driver.Builders.Query.**EQ("_id", "F_S_17460317_0");
>>> var update = Update.AddToSet("**unifiedassociations",
>>> {
>>> "_id" : "A_C_345_S_17460317_0",
>>> "associationid" : 82152889,
>>> "parentkey" : null,
>>> "containerid" : 345,
>>> "containertype" : "C",
>>> "featureid" : 17460317,
>>> "featuretype" : "S",
>>> "cachedate" : ISODate("2012-10-25T15:24:01.**43Z") }
>>> );
>>> _collection.FindAndModify(**query, SortBy.Null, update, false, true);
>>>
>>> This inserts the array properly, with one element added:
>>> {
>>> "_id" : "F_S_17460317_0",
>>> "featureid" : 17460317,
>>> "featuretype" : "S",
>>> "builddate" : ISODate("2012-10-16T13:28:00Z"**),
>>> "lastediteddate" : ISODate("2012-05-31T07:23:37.**963Z"),
>>> "unifiedassociations" :
>>> [
>>> { "_id" : "A_C_345_S_17460317_0",
>>> "associationid" : 82152889,
>>> "parentkey" : null,
>>> "containerid" : 345,
>>> "containertype" : "C",
>>> "featureid" : 17460317,
>>> "featuretype" : "S",
>>> "cachedate" : ISODate("2012-10-25T15:24:01.**43Z")
>>> }
>>> ]
>>> }
>>>
>>> But when I change a field and re-run the query a new record is inserted,
>>> even though the _id's are the same:
>>> {
>>> "_id" : "F_S_17460317_0",
>>> "featureid" : 17460317,
>>> "featuretype" : "S",
>>> "builddate" : ISODate("2012-10-16T13:28:00Z"**),
>>> "lastediteddate" : ISODate("2012-05-31T07:23:37.**963Z"),
>>> "unifiedassociations" :
>>> [
>>> { "_id" : "A_C_345_S_17460317_0",
>>> "associationid" : 82152889,
>>> "parentkey" : null,
>>> "containerid" : 345,
>>> "containertype" : "C",
>>> "featureid" : 17460317,
>>> "featuretype" : "S",
>>> *"cachedate" : ISODate("2012-10-25T14:25:01.43Z")*
>>> },
>>> { "_id" : "A_C_345_S_17460317_0",
>>> "associationid" : 82152889,
>>> "parentkey" : null,
>>> "containerid" : 345,
>>> "containertype" : "C",
>>> "featureid" : 17460317,
>>> "featuretype" : "S",
>>> *"cachedate" : ISODate("2012-10-25T15:11:01.43Z")*
>>> }
>>> ]
>>> }
>>>
>>> I've tried a bunch of different calls, but I can't seem to get it to
>>> work. I'm not even sure if this is possible.
>>>
>>> Help.
>>>
>>>
>
------=_Part_572_21250594.1351249879141
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit
I figured. Is there any way to Upsert the array element purely based on _id? Is the way I've outlined pretty much the best method to perform the Upsert at this time? Is there a better method?<div><br></div><div>Thanks!</div><div><br></div><div>Neal<br><br>On Thursday, October 25, 2012 10:45:05 PM UTC-4, Robert Stam wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Your cachedate values are different, and that's enough for $addToSet to consider it a new element of the unifiedassociations array.<br><br><div class="gmail_quote">On Thu, Oct 25, 2012 at 5:06 PM, Neal Kranes <span dir="ltr"><<a href="javascript:" target="_blank" gdf-obfuscated-mailto="gAYBWD0HOkQJ">nkr...@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I ended up putting a 2 phase Try Update / Else Insert logic:<div><div><font face="courier new, monospace">// Try to update item first</font></div>
<div><font face="courier new, monospace">var query = MongoDB.Driver.Builders.Query.<wbr>And(</font></div><div><font face="courier new, monospace"> MongoDB.Driver.Builders.Query.<wbr>EQ("_id", "</font><span style="font-family:'courier new',monospace">F_S_17460317_0"</span><font face="courier new, monospace">),</font></div>
<div><font face="courier new, monospace"> MongoDB.Driver.Builders.Query.<wbr>EQ("unifiedassociations._id", association._id)</font></div><div><font face="courier new, monospace">);</font></div><div><font face="courier new, monospace">var update = Update.Set("<wbr>unifiedassociations", "[document, removed for brevity]");</font></div>
<div><font face="courier new, monospace">var updateAttemptResult = _featureCollection.Update(<wbr>query, update, SafeMode.True);</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">// If not, insert</font></div>
<div><font face="courier new, monospace">if (updateAttemptResult.<wbr>DocumentsAffected == 0)</font></div><div><font face="courier new, monospace">{</font></div><div><font face="courier new, monospace"> query = MongoDB.Driver.Builders.Query.<wbr>EQ("_id", GetFeatureCacheKey(feature));</font></div>
<div><font face="courier new, monospace"> update = Update.AddToSet("<wbr>unifiedassociations", </font><span style="font-family:'courier new',monospace">"[<wbr>document, removed for brevity]"</span><span style="font-family:'courier new',monospace">);</span></div>
<div><font face="courier new, monospace"> updateAttemptResult = _featureCollection.Update(<wbr>query, update, SafeMode.True);</font></div><div><font face="courier new, monospace">}</font></div><div><br></div>Is this my best bet?</div>
<div><br></div><div>I was hoping to get it done in 1 statement, but after reading a bit it seems that might not be possible.</div><span><font color="#888888"><div><br></div><div>Neal</div></font></span><div>
<div><div><br>On Thursday, October 25, 2012 11:38:10 AM UTC-4, Neal Kranes wrote:<blockquote class="gmail_quote" style="margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><div>I have a document that I want to Upsert an array into. Here's the initial document:</div>
<div><div><font face="courier new, monospace">{</font></div><div><font face="courier new, monospace"> "_id" : "F_S_17460317_0",</font></div><div><font face="courier new, monospace"> "featureid" : 17460317,</font></div>
<div><font face="courier new, monospace"> "featuretype" : "S",</font></div><div><font face="courier new, monospace"> "builddate" : ISODate("2012-10-16T13:28:00Z"<u></u><wbr>),</font></div>
<div><font face="courier new, monospace"> "lastediteddate" : ISODate("2012-05-31T07:23:37.<u></u>9<wbr>63Z")</font></div><div><font face="courier new, monospace">}</font></div></div><div><br></div><div><font face="courier new, monospace">var query = MongoDB.Driver.Builders.Query.<u></u><wbr>EQ("_id", "</font><span style="font-family:'courier new',monospace">F_S_17460317_0</span><font face="courier new, monospace">");</font><br>
</div><div><font face="courier new, monospace">var update = Update.AddToSet("<u></u>unifiedassoci<wbr>ations", </font></div><div><font face="courier new, monospace"> {</font></div><div><font face="courier new, monospace"> "_id" : "A_C_345_S_17460317_0", </font></div>
<div><font face="courier new, monospace"> "associationid" : 82152889, </font></div><div><font face="courier new, monospace"> "parentkey" : null, </font></div><div><font face="courier new, monospace"> "containerid" : 345, </font></div>
<div><font face="courier new, monospace"> "containertype" : "C", </font></div><div><font face="courier new, monospace"> "featureid" : 17460317, </font></div><div><font face="courier new, monospace"> "featuretype" : "S", </font></div>
<div><font face="courier new, monospace"> "cachedate" : ISODate("2012-10-25T15:24:01.<u></u>4<wbr>3Z") }<br></font></div><div><font face="courier new, monospace"> );</font><br></div><div><font face="courier new, monospace">_collection.FindAndModify(<u></u>quer<wbr>y, SortBy.Null, update, false, true);<br>
</font></div><div><br></div><div>This inserts the array properly, with one element added:</div><div><div><font face="courier new, monospace">{</font></div><div><font face="courier new, monospace"> "_id" : "F_S_17460317_0",</font></div>
<div><font face="courier new, monospace"> "featureid" : 17460317,</font></div><div><font face="courier new, monospace"> "featuretype" : "S",</font></div><div><font face="courier new, monospace"> "builddate" : ISODate("2012-10-16T13:28:00Z"<u></u><wbr>),</font></div>
<div><font face="courier new, monospace"> "lastediteddate" : ISODate("2012-05-31T07:23:37.<u></u>9<wbr>63Z"),</font></div><div><div><font face="courier new, monospace"> "unifiedassociations" : </font></div>
<div><span style="font-family:'courier new',monospace"> [</span></div><div><font face="courier new, monospace"> { "_id" : "A_C_345_S_17460317_0", </font></div><div><font face="courier new, monospace"> "associationid" : 82152889, </font></div>
<div><font face="courier new, monospace"> "parentkey" : null, </font></div><div><font face="courier new, monospace"> "containerid" : 345, </font></div><div><font face="courier new, monospace"> "containertype" : "C", </font></div>
<div><font face="courier new, monospace"> "featureid" : 17460317, </font></div><div><font face="courier new, monospace"> "featuretype" : "S", </font></div><div><font face="courier new, monospace"> "cachedate" : ISODate("2012-10-25T15:24:01.<u></u>4<wbr>3Z") </font></div>
<div><font face="courier new, monospace"> }<br></font></div><div><font face="courier new, monospace"> ]</font></div></div><div><font face="courier new, monospace">}</font></div></div><div><font face="courier new, monospace"><br>
</font></div><div>But when I change a field and re-run the query a new record is inserted, even though the _id's are the same:</div><div><div><font face="courier new, monospace">{</font></div><div><font face="courier new, monospace"> "_id" : "F_S_17460317_0",</font></div>
<div><font face="courier new, monospace"> "featureid" : 17460317,</font></div><div><font face="courier new, monospace"> "featuretype" : "S",</font></div><div><font face="courier new, monospace"> "builddate" : ISODate("2012-10-16T13:28:00Z"<u></u><wbr>),</font></div>
<div><font face="courier new, monospace"> "lastediteddate" : ISODate("2012-05-31T07:23:37.<u></u>9<wbr>63Z"),</font></div><div><div><font face="courier new, monospace"> "unifiedassociations" : </font></div>
<div><span style="font-family:'courier new',monospace"> [</span></div><div><div><font face="courier new, monospace"> { "_id" : "A_C_345_S_17460317_0", </font></div><div><font face="courier new, monospace"> "associationid" : 82152889, </font></div>
<div><font face="courier new, monospace"> "parentkey" : null, </font></div><div><font face="courier new, monospace"> "containerid" : 345, </font></div><div><font face="courier new, monospace"> "containertype" : "C", </font></div>
<div><font face="courier new, monospace"> "featureid" : 17460317, </font></div><div><font face="courier new, monospace"> "featuretype" : "S", </font></div><div><font face="courier new, monospace"> <b>"cachedate" : ISODate("2012-10-25T14:25:01.<u></u>4<wbr>3Z")</b> </font></div>
<div><font face="courier new, monospace"> },<br></font></div></div><div><div><font face="courier new, monospace"> { "_id" : "A_C_345_S_17460317_0", </font></div><div><font face="courier new, monospace"> "associationid" : 82152889, </font></div>
<div><font face="courier new, monospace"> "parentkey" : null, </font></div><div><font face="courier new, monospace"> "containerid" : 345, </font></div><div><font face="courier new, monospace"> "containertype" : "C", </font></div>
<div><font face="courier new, monospace"> "featureid" : 17460317, </font></div><div><font face="courier new, monospace"> "featuretype" : "S", </font></div><div><font face="courier new, monospace"> <b>"cachedate" : ISODate("2012-10-25T15:11:01.<u></u>4<wbr>3Z")</b> </font></div>
<div><font face="courier new, monospace"> }</font></div></div><div><font face="courier new, monospace"> ]</font></div></div><div><font face="courier new, monospace">}</font></div></div><div><br></div><div>I've tried a bunch of different calls, but I can't seem to get it to work. I'm not even sure if this is possible.</div>
<div><br></div><div>Help.</div><div><br></div></blockquote></div></div></div></blockquote></div><br>
</blockquote></div>
------=_Part_572_21250594.1351249879141--
------=_Part_571_23511527.1351249879141--