Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Projection of mongodb subdocument using C# .NET driver 2.0

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?

like image 697
Lynchy Avatar asked Apr 18 '15 17:04

Lynchy


People also ask

How do I project a single field in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});

What is projection query in MongoDB?

In MongoDB, projection means selecting only the necessary data rather than selecting whole of the data of a document. If a document has 5 fields and you need to show only 3, then select only 3 fields from them.


2 Answers

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.

like image 186
Craig Wilson Avatar answered Oct 01 '22 22:10

Craig Wilson


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.

like image 31
Avish Avatar answered Oct 01 '22 20:10

Avish