0

I have example entry in my database:

{
  "_id": {
    "$oid": "6241d974a7382732093700d1"
  },
  "userdata": {
    "Name": "SomeName",
    "Surname": "SomeSurname",
    "Email": "someEmail@gmail.com",
    "Adress": {
      "Ulica": "Street",
      "NumerDomu": "2137",
      "NumerMieszkania": "2137",
      "KodPocztowy": "22222",
      "Miasto": "Warsaw"
    }
  },
  "username": "usrnmatest",
  "password": "$2a$11$kLDNbOmg2Ju4jl4o7mcyyuThuE5ZN1xd4VB10TYI/xPtQO.zM828a",
  "cards": [
    {
      "issuer": "Visa",
      "cardNumber": "46556456445564",
      "expiryMonth": "12",
      "expiryYear": "2027",
      "CVV": "223",
      "name": "Pablo",
      "address": "Krynica 21",
      "country": "Poland",
      "zipcode": "33-380"
    },
    {
      "issuer": "MasterCard",
      "cardNumber": "2178811445564",
      "expiryMonth": "10",
      "expiryYear": "2032",
      "CVV": "114",
      "name": "Some Data",
      "address": "ul.Moskiewskiego 21",
      "country": "Poland",
      "zipcode": "33-380"
    }
  ]
}

I want to update, for example card by it's cardNumber. I wrote something like this:

var arrayFilter = Builders<User>.Filter.Eq("Id", user.Id)
                & Builders<User>.Filter.Eq("cardNumber", card.cardNumber);
            var arrayUpdate = Builders<User>.Update.Set("cards.$", card);
            var res = _userCollection.UpdateOne(arrayFilter, arrayUpdate);

But I'm getting 0 modified fields.

I kind of followed this tutorial Tutorial Link Any idea how to make it work?

Also here is my model of User:

    public class User
    {
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string? Id { get; set; }

        [BsonRequired]
        [BsonElement("userdata")]
        [Required]
        public UserData? DaneUzytkownika { get; set; }

        [BsonElement("username")]
        [BsonRequired]
        [Required]
        public string UserName { get; set; } = null!;

        [BsonElement("password")]
        [BsonRequired]
        [Required]
        public string Password { get; set; } = null!;

        [BsonElement("cards")]
        public List<Card>? cards { get; set; }

    }

And my Card model if this can help:

    public class Card
    {
        public string? issuer { get; set; }

        [BsonElement("cardNumber")]
        public string? cardNumber { get; set; }

        public string? expiryMonth { get; set; }

        public string? expiryYear { get; set; }

        public string? CVV { get; set; }

        public string? name { get; set; }

        public string? address { get; set; }

        public string? country { get; set; }

        public string? zipcode { get; set; }


    }
Pan Michal
  • 177
  • 1
  • 3
  • 15
  • this maybe of help: https://stackoverflow.com/questions/48180188/c-sharp-mongodb-update-an-element-inside-a-nested-document – R2D2 Apr 01 '22 at 19:35
  • Should `"Id"` be `"_id"` in your filter? Admittedly, I'm not sure, it's been a few months since I used MDB .NET driver. – Kit Apr 01 '22 at 21:46
  • Guess MBD Driver just translate it because I have simillar method above in this controller that works with "Id" – Pan Michal Apr 02 '22 at 09:56

1 Answers1

0

You need to fix the order of elements in your update query , the correct is:

 UpdateOne(filter, update, updateOptions)

and the arrayFilters usually is located in the updateOptions:

db.collection.update({
  // here filter the document that need to be updated
 {
   "_id": {
    "$oid": "6241d974a7382732093700d1"
   }
 },
{
 // here change the necessary values inside the cards array element
 $set: {
   "cards.$[x].CVV": "Something in the cards that need to be updated"
}
},
{
 // and this is where you define the arrayFilters 
  arrayFilters: [
   {
     "x.cardNumber": "46556456445564"
   }
  ]
 })

example playground via JS mongo shell

R2D2
  • 9,410
  • 2
  • 12
  • 28