[CSHARP-1585] Support for Filters without name in C# Driver Created: 01/Mar/16  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.2.3
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Saleem Assignee: Robert Stam
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows, Mac, C#, MongoDB 3.2, C# Driver 2.2.3 and earlier.


Issue Links:
Related
is related to CSHARP-1107 Query.LT, LTE and other similar metho... Closed

 Description   

I'm trying to translate following MongoDB shell query into C# query.

db.col.find({"Details.a":{$elemMatch:{$elemMatch:{DeviceName : /.Name0./}}}});

As you can see $elemMatch is nested into another $elemMatch. This query runs perfectly in shell.
I have no clue how to translate it to C# as driver doesn't accept a filter without a name.
I'm trying following code:

Builders<BsonDocument>.Filter.ElemMatch ("Details.a", Builders<BsonDocument>.Filter.ElemMatch 
(Builders<BsonDocument>.Filter.Regex(Builders<BsonDocument>.Filter.Regex
("DeviceName",new BsonRegularExpression("")))));

No doubt, it didn't worked.
I'm wondering if you guys have any plan to add support to driver to accept filters without name?



 Comments   
Comment by Robert Stam [ 07/Apr/21 ]

My apologies for taking so long to come back to this. As part of our effort to reimplement our LINQ translator we are investigating old tickets to see how the new LINQ translator handles the reported issue.

Using the new LINQ implementation the following query expression:

 document => document.Details.A.Any(x => x.Any(y => Regex.IsMatch(y.DeviceName, @".Name0.")))

Translates to the following filter

 { 'Details.A' : { $elemMatch : { $elemMatch : { DeviceName : /.Name0./ } } } }

which matches what you say you want.

I used the following reverse engineered class definitions to write that query against:

 public class Document
{
    public int Id { get; set; }
    public Details Details { get; set; }
}
 
public class Details
{
    public Device[][] A;
}
 
public class Device
{
    public string DeviceName { get; set; }
}

Comment by Saleem [ 01/Mar/16 ]

Thank you Craig for prompt response. I have used following solution for now but hoping MongoDB will bring C# API upto level of shell APIs sooner.

var bsonQuery = "{'Details.a':{$elemMatch:{$elemMatch:{DeviceName : /.*Name0.*/}}}}";
var filter = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(bsonQuery);
 
var result = col.FindSync (filter).ToList();

Comment by Craig Wilson [ 01/Mar/16 ]

Hi Saleem,

We are aware of this issue. It's been a long-standing issue even with the old API (CSHARP-1107). All we can suggest at this time is, when you need this functionality, use a BsonDocument instead of the builders.

Generated at Wed Feb 07 21:40:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.