Update doesn't seem to be updating...

80 views
Skip to first unread message

Howard van Rooijen

unread,
Apr 3, 2010, 1:16:41 PM4/3/10
to norm-m...@googlegroups.com
Hello,

I've started to put together a small demo that I was going to use to blog about getting started with MongoDB & Norm. It's a very simple application that downloads a government data feed of clubs, converts the feed to a collection of POCO's then inserts them into MongoDB via NoRM - this all work find and is quite mind blowingly simple to get running (NoRM is so slick). 

Then I decided to add a second scenario whereby you update the contact number of one of the clubs, send the update to MongoDB then re-query - but for some reason the update is not persisted.

I've attached the sample - it uses the Session object from the Norm.Tests - as I thought that was a neat wrapper. Does anyone have any ideas why it's not updating? I've stepped through all the code and nothing seems amiss - but I don't know of any ways to intercept / debug MongoDB.

Many thanks,

Howard
getting-started-with-mongodb.zip

Olivier Oswald

unread,
Apr 3, 2010, 2:31:07 PM4/3/10
to Howard van Rooijen, norm-m...@googlegroups.com
I am doing this to update an entity:
       session.Provider.DB.GetCollection<Account>().UpdateOne(new { _id = account.ID }, account);

because the session's Update() method has no effect. I believe it matches the entire updated document against the database, which would never return any results (unless you haven't changed your original entity). Bug?

***
        public void Update<T>(T item) where T : class, new()
        {
            _provider.DB.GetCollection<T>().UpdateOne(item, item);
        }
       // UpdateOne Signature: void UpdateOne<X, U>(X matchDocument, U valueDocument)



--
You received this message because you are subscribed to the Google Groups "NoRM mongodb" group.
To post to this group, send email to norm-m...@googlegroups.com.
To unsubscribe from this group, send email to norm-mongodb...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/norm-mongodb?hl=en.

Howard van Rooijen

unread,
Apr 3, 2010, 4:50:09 PM4/3/10
to Olivier Oswald, norm-m...@googlegroups.com
Excellent - thanks for that - I ended up making a small adjustment - I created a new interface:

public interface IUniqueIdentifier
{
    ObjectId Id { get; set; }
}

and then ensured that my POCO implemented it then I could implement your workaround in the original session object:

public void Update<T>(T item) where T :  class, IUniqueIdentifier, new()
{
    this.provider.DB.GetCollection<T>().UpdateOne(new { _id = item.Id }, item);
}

and this now displays the correct behaviour.

Many thanks,

Howard

karl

unread,
Apr 3, 2010, 6:44:39 PM4/3/10
to NoRM mongodb
You might be looking for the Save method.

Save is a "smart" wrapper around Update and Insert...so it'll try to
figure out whether you want to save or insert, and for an update,
it'll generate the proper query to update just that instance.

On Apr 3, 4:50 pm, Howard van Rooijen <howard.vanrooi...@gmail.com>
wrote:


> Excellent - thanks for that - I ended up making a small adjustment - I
> created a new interface:
>
> public interface IUniqueIdentifier
> {
>     ObjectId Id { get; set; }
>
> }
>
> and then ensured that my POCO implemented it then I could implement your
> workaround in the original session object:
>
> public void Update<T>(T item) where T :  class, IUniqueIdentifier, new()
> {
>     this.provider.DB.GetCollection<T>().UpdateOne(new { _id = item.Id },
> item);
>
> }
>
> and this now displays the correct behaviour.
>
> Many thanks,
>
> Howard
>

> >> norm-mongodb...@googlegroups.com<norm-mongodb%2Bunsu...@googlegroups.com>

Michel

unread,
Apr 6, 2010, 11:12:21 AM4/6/10
to NoRM mongodb
Bonjour,

The session's Update() method "don't work" for me too. To use the
following code :

session.Update(originalContact, contactToEdit);

I had to add another Update method :

public void Update<T>(T match, T value) where T : class, new()
{
_provider.DB.GetCollection<T>().UpdateOne(match, value);
}

You must pass the original object and the updated object to the Update
method so that the update work properly.

Michel

Michael Kennedy

unread,
Apr 12, 2010, 11:56:26 PM4/12/10
to NoRM mongodb
Hi guys,

I ran into the same problem. Update had no effect. I was using the
model in the test project

public void Update<T>(T entity) where T : class, new()
{
provider.DB.GetCollection<T>().UpdateOne( entity, entity );
}

Then your hints above got it working for me:

public void Update<T>(T entity) where T : IHasIdentifier, new()
{
provider.DB.GetCollection<T>().UpdateOne( new {_id = entity._id},
entity );
}

where IHasIdentifier is defined as:

interface IHasIdentifier
{
ObjectId _id {get; set;}
}

The benefit of defining the property like that (rather than say ID) is
when you insert it, the entity automatically get's its mongo generated
ID set. Then using "new {_id = entity._id}" as the match criteria
seemed to work fine.

Hope this helps someone out there.

Best regards,
Michael

Michael Kennedy
http://www.michaelckennedy.net
@mkennedy

On Apr 6, 8:12 am, Michel <mich...@gmail.com> wrote:
> Bonjour,
>
> The session'sUpdate() method "don't work" for me too. To use the


> following code :
>
> session.Update(originalContact, contactToEdit);
>
> I had to add anotherUpdatemethod :
>

> public voidUpdate<T>(T match, T value) where T : class, new()

Reply all
Reply to author
Forward
0 new messages