0

I've added a method to a MongoDB database repository class, that is supposed to upsert/update documents.

But I noticed after calling the UpdateCustomer() on a document, a new one is created and the old is unmodified. In short, the upsert method is behaving like a save new method.

In order to debug the issue I checked the filter being used, which seems to be correct:

 var filter = Builders<CustomerModel>.Filter.Where(x => x.Id == customer.Id);

But when I step into the result, it shows a MatchedCount and ModifiedCount of zero. Indicating that filter doesn't match the existing document.

I also checked that the ID's of both the original and modified documents are the same, which they are:

id match

Does anyone know why the document isn't being updated in this instance?

The complete UpdateCustomer method is as follows, the customer object is the SelectedCustomer from my VM which has been modified.:

    public async Task UpdateCustomer(CustomerModel customer)
    {          
        var collection = StartConnection();
        var filter = Builders<CustomerModel>.Filter.Where(x => x.Id == customer.Id);

        var result = await collection.ReplaceOneAsync(filter, customer, new UpdateOptions { IsUpsert = true });
    }

My documents are defined as follows on my remote DB (last two records showing the same ID number):

customers collection

And when I call a toString() on my filter, the following query is created:

find({ \"_id\" : ObjectId(\"5565d8adba02d54a4a78be93\") })

This is the CustomerModel class as reuqested:

namespace MongoDBApp.Models
{
    public class CustomerModel : INotifyPropertyChanged
    {

        private ObjectId id;
        private string firstName;
        private string lastName;
        private string email;


        /// <summary>
        /// This attribute is used to map the Id property to the ObjectId in the collection
        /// </summary>
        [BsonId]
        public ObjectId Id 
        {
            get
            {
                return id;
            }
            set
            {

                id = value;
            }
        }

        [BsonElement("firstName")]
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                firstName = value;
                RaisePropertyChanged("FirstName");
            }
        }

        [BsonElement("lastName")]
        public string LastName
        {
            get
            {
                return lastName;
            }
            set
            {
                lastName = value;
                RaisePropertyChanged("LastName");
            }
        }

        [BsonElement("email")]
        public string Email
        {
            get
            {
                return email;
            }
            set
            {
                email = value;
                RaisePropertyChanged("Email");
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
Brian Var
  • 6,029
  • 25
  • 114
  • 212
  • That's... weird... What is the type of your identifier? Are you sure they are both the same type in the database? Also, could you provide the mongo query syntax that is getting generated by your filter? (collection.Find(filter).ToString()) – Craig Wilson Nov 25 '15 at 14:03
  • The id is of type ObjectID, link to docs in db: http://picpaste.com/pics/customers_collection_ss-jTRdJbLL.1448462069.png You can see that the last two records, the original and updated have the same id. Where as the previous should have been deleted on the update.This is the query generated by my filter: "object.ToString returned "find({ \"_id\" : ObjectId(\"5565d8adba02d54a4a78be93\") })" string Maybe it's an issue with the way I've set up the object id's in my remote db? " @CraigWilson – Brian Var Nov 25 '15 at 14:34
  • Could you paste a CustomerModel class? Especially, I think about _id field. – marcinax Nov 25 '15 at 21:01
  • just added the Model now @marcinax – Brian Var Nov 25 '15 at 21:24

1 Answers1

0

The solution to the null match count on record update, was due to an incorrect format of the ObjectID in the document on the database.

Incorrect format:

{   
     "_id": "565737b6e45de21ac4fd17a5"
    "firstName": "Joe ",
    "lastName": "Doe",
    "email": "jd@outlook.com"
}

I noticed that when I created a new record, the json for the document's id was specified in a different format.

So the correct format, that led to a valid update was:

{
    "_id": {
        "$oid": "565737b6e45de21ac4fd17a5"
    },
    "firstName": "Joe ",
    "lastName": "Doe",
    "email": "jd@outlook.com"
}
Brian Var
  • 6,029
  • 25
  • 114
  • 212