[CSHARP-1327] Mongo Database queries in c#.net Created: 19/Jun/15  Updated: 05/Apr/19  Resolved: 22/Jun/15

Status: Closed
Project: C# Driver
Component/s: API
Affects Version/s: 2.0.1
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Arvind Chary Assignee: Unassigned
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows


Attachments: PNG File 2.png    

 Description   

I am using Mongo CSharpDriver-2.0.0 version and IMongoCollection Async in my application. My data collection looks as shown in the figure attached(2.png).
Here I want to perform a Search operation on Categories, basically we will give comma separated list of Category Names and it should return a list of Records that are matching the categories that are provided as input.
To implement above functionality I am using the below code: This code works fine when I pass a single category.

var collection = _database.GetCollection<BusinessDeal>("BusinessDeals");
var filter = Builders<BusinessDeal>.Filter.Eq("Categories.CategoryName", categorylist) | Builders<BusinessDeal>.Filter.Eq("Accessgroups.AccessgroupName", groups);
var result = await collection.Find(filter).ToListAsync();

But my requirement is to get the records when I provide a Comma separated categories List. For that implementation I have implemented the following code:
var collection = _database.GetCollection<BusinessDeal>("BusinessDeals");
var filter = Builders<BusinessDeal>.Filter.AnyIn("Categories.CategoryName", categorylist.Split(',').ToArr()) |
Builders<BusinessDeal>.Filter.AnyIn("Accessgroups.AccessgroupName", groups.Split(',').ToArr());
var result = await collection.Find(filter).ToListAsync();
The above code is not returning any results and throwing error as “Not Yielded any Results” and Status message as “Failed”.



 Comments   
Comment by Arvind Chary [ 22/Jun/15 ]

Hello Craig,
I was able to successfully implement the fix that you have provided to me. Today I have tried to post an issue on google group according to your suggestion, I was able create the post successfully without any issues. But when I completed my composing the post and clicked on the "Post" button, I have not got any message saying that My post was created or not. So I have created a new post on JIRA (https://jira.mongodb.org/browse/CSHARP-1330). Please go through this and help me.

Thanks,
Arvind.

Comment by Craig Wilson [ 19/Jun/15 ]

Hi Arvind,

For future reference, questions such as this are better asked on stackoverflow or our discussion list: https://groups.google.com/forum/?pli=1#!forum/mongodb-user.

First off, you'll want to use just In instead of AnyIn. Other than that, have you taken into account that your comma separated list might have whitespace? "One, Two" instead of "One,Two"?

Second, you can see exactly what query is getting generated by using ToString() on the result of the Find:

var query = collection.Find(filter);
var queryString = query.ToString();
var result = await query.ToListAsync();

It would be helpful to know what query is actually getting generated and, if it looks correct, does it run in the shell?

Craig

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