[CSHARP-2699] Wrong queries for nested filters Created: 20/Aug/19  Updated: 28/Oct/23  Resolved: 20/Sep/19

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.8.0, 2.8.1, 2.9.0
Fix Version/s: 2.9.2

Type: Bug Priority: Major - P3
Reporter: Ruslan Shakirov Assignee: Dmitry Lukyanov (Inactive)
Resolution: Fixed Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File Program.cs    
Issue Links:
Related
related to CSHARP-2744 Consider to restore(or modify) the pr... Closed
Case:

 Description   

The linq expression:

var list = collection.AsQueryable()
                    .Where(city =>
                        city.Shops.Any(shop => shop.Name == "Shop 1" && shop.Books.Any(book => book.Pages.Any(page => page.Text == "page_1_1"))))
                    .ToList();

Generated queries (from `CommandStartedEvent.ToJson()`)
v 2.7.3

aggregate - {
	"aggregate": "cities",
	"pipeline": [{
			"$match": {
				"Shops": {
					"$elemMatch": {
						"Name": "Shop 1",
						"Books.Pages.Text": "page_1_1"
					}
				}
			}
		}
	],
	"cursor": {},
	"$db": "test",
	"lsid": {
		"id": CSUUID("db90a3a1-e153-44a5-97dd-64660ba367f0")
	}
}

v.2.8.0+

aggregate - {
	"aggregate": "cities",
	"pipeline": [{
			"$match": {
				"Shops": {
					"$elemMatch": {
						"Name": "Shop 1",
						"Pages": {
							"$elemMatch": {
								"Text": "page_1_1"
							}
						}
					}
				}
			}
		}
	],
	"cursor": {},
	"$db": "test",
	"lsid": {
		"id": CSUUID("01211669-37e4-4dd3-b838-6efe1a8043ed")
	}
}

The `Books` element is missing in query generated by driver v2.8.0+, so it returns wrong result.



 Comments   
Comment by Githook User [ 25/Sep/19 ]

Author:

{'name': 'Dmitry Lukyanov', 'username': 'DmitryLukyanov', 'email': 'dmitry.lukyanov@mongodb.com'}

Message: CSHARP-2699: Fix an $elemMatch query for nested Anys.
Branch: v2.9.x
https://github.com/mongodb/mongo-csharp-driver/commit/9335995409078ce9a9f2506d56a05bead53e200d

Comment by Githook User [ 20/Sep/19 ]

Author:

{'username': 'DmitryLukyanov', 'email': 'dmitry.lukyanov@mongodb.com', 'name': 'Dmitry Lukyanov'}

Message: CSHARP-2699: Fix an $elemMatch query for nested Anys.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/85c5f480945895d61ce77ddfcfdd6f09f53da615

Comment by Ruslan Shakirov [ 20/Aug/19 ]

It works ok if linq expression contains condition with nested filter only:
city.Shops.Any(shop => shop.Books.Any(book => book.Pages.Any(page => page.Text == "page_1_1"))

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