The CartItems field has products of the same type I want to remove just one entry of the product but my code removes all the entries as the filter is satisfied by all the products.
public class ProductCarted
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; set; }
[BsonElement("id")]
public int SecondID { get; set; }
[BsonElement("title")]
public string Title { get; set; } = null!;
public string Description { get; set; } = null!;
public int Price { get; set; }
public double DiscountPercentage { get; set; }
public double Rating { get; set; }
public string Brand { get; set; } = null!;
public string Category { get; set; } = null!;
public string Thumbnail { get; set; } = null!;
public string[] Images { get; set; } = null!;
}
public class Cart
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string? SessionId { get; set; }
public List<ProductCarted> CartItems { get; set; } = new List<ProductCarted>();
}
public async Task<UpdateResult> RemoveOneProduct(string sessionId, ProductCarted p)
{
var filter = Builders<Cart>.Filter.Eq(c => c.SessionId, sessionId);
var update = Builders<Cart>.Update.PullFilter(c => c.CartItems, ci => ci.Id == p.Id);
var res = await _cartsCollection.UpdateOneAsync(filter, update);
return res;
}