"Update if Current"

25 views
Skip to first unread message

Fabrizio Accatino

unread,
Sep 30, 2010, 4:05:40 AM9/30/10
to mongodb...@googlegroups.com
"Update if current" from MongoDB documentation:


I'm trying to use this approach with mondodb-csharp. Is there a built-in way to do it or I need to do it "manually"?

Actually I manage it "manually": 

Person p = collection.FindOne(...)   // get person from db collection ...
Person p_original = p.clone()    // ... my method...
p.Name = "new name";
collection.Update(p, p_original);

// check update
Document error = db.GetLastError();
if ((bool)error["updatedExisting"] == false)
    Console.WriteLine("ERROR");
else
    Console.WriteLine("OK");


Is there a way to check the update result without querying DB.GetLastError() ?
collection.Update(p, p_original) does not fail if it update nothing. Is it the right behaviour?  ... I'd like to get an exception....

  thank you 

fabrizio

Steve Wagner

unread,
Sep 30, 2010, 4:47:09 AM9/30/10
to mongodb...@googlegroups.com
Hi Fabrizio, it seems this is new since i dose not noticed that this
exists before. I will think about how we could add that. Can you file
the support for UpdatedExisting to our bugtracker at MongoDB Jira?

-Steve

Fabrizio Accatino

unread,
Sep 30, 2010, 10:03:12 AM9/30/10
to mongodb...@googlegroups.com
Steve,

I'll add a ticket on Jira this evening. 

In the meantime, I'd like to share a bit more what I'd like to do.  :)

I need to implement "optimistic concurrency". So my steps are:
 - read object from DB
 - create a copy
 - modify values
 - update db using both object and original object
 - I'll get true or false 



Actually my code is:

Person p = personColl.FindOne(...);
Person p_original = p.SimpleClone();
p.Name = "new_name";
bool result = personColl.UpdateOptimicticConcurrency(p, p_original);


public static bool UpdateOptimicticConcurrency<T>(this IMongoCollection<T> collection, T newObject, T oldObject) where T : class
{
    collection.Update(newObject, oldObject);
    Document error = collection.Database.GetLastError();
    return (bool)error["updatedExisting"];
}

Fabrizio Accatino

unread,
Sep 30, 2010, 10:57:48 AM9/30/10
to mongodb...@googlegroups.com
Person p = personColl.FindOne(...);
Person p_original = p.SimpleClone();

Steve, my SimpleClone() is very simple (and stupid). So.... is there a way to get another instace of Person without querying the DB again?
I try to explain. When I call FineOne() or Find() or similar methods mongodb-csharp executes a query. With the result it build an object, in my case an instance of Person. Is there a way to get 2 instance of Person with only one query to DB? Ok, ok, it's a "hack"...  :)  can you give some points?

I cannot do:
Person p = personColl.FindOne(...);
Person p_orig = personColl.FindOne(...);

beacuse in a  heavy multiuser environment I'm not secure if somebody modify data on DB between the two FineOne() calls.  


fabrizio

Steve Wagner

unread,
Sep 30, 2010, 1:27:49 PM9/30/10
to mongodb...@googlegroups.com
The simples way in .Net to make a deep clone is to use the
BinarySerializer and serialize it back. Just make sure the object gets a
new Id before you save it.

No as far as in know there is now way to get the same object twice in
one query. But query MongoDB by id is very very fast.

As i say, the better way it to use the BinarySerializer.

-Steve

Fabrizio

unread,
Oct 7, 2010, 3:14:05 AM10/7/10
to mongodb-csharp
Reply all
Reply to author
Forward
0 new messages