17

I have the following structure:

public class Category
{
    [BsonElement("name")]
    public string CategoryName { get; set; }

    [BsonDateTimeOptions]
    [BsonElement("dateCreated")]
    public DateTime DateStamp { get; set; }

    [BsonElement("tasks")]        
    public List<TaskTracker.Task> Task { get; set; }
}

public class Task
{
    [BsonElement("name")]
    public string TaskName { get; set; }

    [BsonElement("body")]
    public string TaskBody { get; set; }
}

I am trying to query a Category to get all the TaskName values and then return them to a list to be displayed in a list box.

I have tried using this query:

var getTasks = Categories.Find<Category>(x => x.CategoryName == catName)
                         .Project(Builders<Category>.Projection
                                                    .Include("tasks.name")
                                                    .Exclude("_id"))
                         .ToListAsync()
                         .Result;   

But what get returned is: {"tasks": [{"name: "test"}]}.

Is there anyway to just return the string value?

2 Answers 2

33

As Avish said, you have to use the aggregation API to get the resulting document to look like you are wanting. However, the driver can make some of that disappear for you if you use the expression tree API for project as you have done for Find. For instance, I believe the following should work for you:

var taskNames = await Categores.Find(x => x.CategoryName == catName)
    .Project(x => x.Tasks.Select(y => y.Name))
    .ToListAsync();

This should just bring back an enumerable of strings (tasks.name) for each category. The driver will be inspecting this projection and only pull back the tasks.name field.

Sign up to request clarification or add additional context in comments.

7 Comments

I have tried your query and have tried adding it to a List<IEnumerable<string>> but what gets returned is: '{System.Linq.Enumerable.WhereSelectEnumerableIterator<MongoDB.Driver.Linq.Translators.ProjectedObject,string>}'
Yes, that is convertible to an IEnumerable<string>. Could you provide some more code about what you are doing?
I think I have found a solution. I returned taskNames query like this: return taskNames[0].ToList(); and that seemed it work. Is this good practice?
Sure... at his point, all the data is in memory, so do with it what you want.
There is. This answer demonstrates exactly that. It will only bring back the "tasks.name" field.
|
10

MongoDB doesn't really support projections the way SQL databases do; you can ask for a partial document, but you'd still get back something that matches the schema of the document you were querying. In your case, you're getting back only the tasks field, and for each task, only the name field.

You can easily transform this into a list of strings using plain LINQ:

var categoryTasks = Categories.Find<Category>(x => x.CategoryName == catName)
                     .Project(Builders<Category>.Projection
                                                .Include("tasks.name")
                                                .Exclude("_id"))
                     .ToListAsync()
                     .Result;   

var taskNames = categoryTasks.Tasks.Select(task => task.Name).ToList();

Alternatively, you can do some fancy stuff with the aggregations API (which does support custom projections, kinda), but that would probably be overkill for you case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.