[CSHARP-2538] MatchedCount not considering ArrayFilters Created: 04/Mar/19  Updated: 27/Oct/23  Resolved: 01/Apr/19

Status: Closed
Project: C# Driver
Component/s: BSON
Affects Version/s: 2.7.3
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Arun Mahendrakar Assignee: Wan Bachtiar
Resolution: Works as Designed Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

windows



 Description   

Here's a model I have:

Customer

  • CustomerNumber (unique identifier of the document)
     - PhysicalAddresses (collection)
       - AddressId (unique identifier of each address)
       - AddressStatus

I want to update the AddressStatus to either active or inactive. But if I'm unable to find this address, I'd like to add to the collection.

Accordingly, I'm doing a $set to modify the AddressStatus. My filter will have the customer number and I have an array filter which has the AddressId condition.

Filter: var builder = Builders<Customer>.Filter;var builder = Builders<Customer>.Filter;
var filter = builder.Eq(Constants.CustomerNumber, "");

Update: { "$set" :

{ "PhysicalAddresses.$[cl1].Status" : "Inactive"}

}

ArrayFilter: var arrayFilterDefinitions = new List<BsonDocumentArrayFilterDefinition<object>>();
var arrayFilterDefinitions = new List<BsonDocumentArrayFilterDefinition<object>>(); arrayFilterDefinitions.Add(new BsonDocumentArrayFilterDefinition<object>(new BsonDocument("el1.AddressId", "98875")));

This works fine if the address exists. If it doesn't, the UpdateResult response I get is:

IsAcknowledged: true
MatchedCount: 1
ModifiedCount: 0

My question is why is the MatchedCount = 1. Is it because it found the document that was part of the filter? If so, that means the ArrayFilter is not considered towards the match.

In my opinion, even the array filter is part of the filter itself, so MatchedCount should be more than 0, only if filter AND array filter conditions are satisfied.

Any reason why this is not the case?

Thanks,

Arun



 Comments   
Comment by Wan Bachtiar [ 25/Mar/19 ]

My question is why is the MatchedCount = 1. Is it because it found the document that was part of the filter? If so, that means the ArrayFilter is not considered towards the match.

Hi Arun,

Yes, the MatchedCount is a count based on the filter part of the update operation. That's also correct, the ArrayFilter is not considered part of the matched documents, this is because ArrayFilter is supposed to match on a document level not on the collection level.

This behaviour is defined in MongoDB server for arrayFilters, and not defined by the drivers.

If you would like to upsert a document, that doesn't exist yet, you could do something as below:

var filter = Builders<BsonDocument>.Filter.Eq("PhysicalAddresses.AddressId", "98875");
var update = Builders<BsonDocument>.Update.Set("PhysicalAddresses.Status", "Inactive");
var updateOptions = new UpdateOptions { IsUpsert = true };
var result = collection.UpdateOne(filter, update, updateOptions);

Although this would only add a document with value below i.e. there is no CustomerNumber value.

{{PhysicalAddresses:[{AddressId: "98875", Status:"Inactive"}]}} 

 

Please note that CSHARP project is for reporting bugs or feature suggestions for the MongoDB .NET/C# driver. If you have any follow-up questions on the use of the MongoDB server or C# driver, please post a question on mongodb-user group with relevant the information.

Regards,
Wan.

Comment by Jeffrey Yemin [ 08/Mar/19 ]

Hi nmarun

It's in our queue to investigate. We will get to it as soon as we can!

Regards,
Jeff

Comment by Arun Mahendrakar [ 08/Mar/19 ]

Hi Ian, I have not received any update on this ticket yet. Can you please
provide me with an ETA?

Thanks
Arun

Comment by Arun Mahendrakar [ 08/Mar/19 ]

@Wan, sir, please let me know if you need any other clarification on my question.

Comment by Arun Mahendrakar [ 04/Mar/19 ]

Sorry, there are a couple of typos in the above:

  1. The CustomerNumber and PhysicalAddresses are at the same level in the document. It shows PhysicalAddresses indented to me here.
  2. I am passing a customer number in the filter though it is showing as "" in the question.
  3. The identifier is "cl1" in both the Update and the ArrayFilter.
Generated at Wed Feb 07 21:42:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.