Given this doc form a collection of Courses I would like to pick a
single participant object from the embedded array
{
_id: someCourseId,
CourseName: "someName",
Participants[
{UserId: X Name: "person1"},
{UserId:Y, Name:"person2"},
{UserID:Z, Name:"person3"}
]
}
How do I write a query with the official C# sharp driver that only
returns the embedded array Element with userID Y ?
Right now I can select the Courses having a Participant with id Y
using Query.EQ(“Participants.UserId”,Y) and I can select to only get
the Participants field of the Course object using
SetFields(“Participants”), but if this array is huge, I don’t want to
fetch an entire array, but only a single element. Is this possible?
If not, then I would consider it best practice to not use embedded
arrays in cases where fetching specific elements are required.
Instead a separate collection should be used to hold the array values
and a “foreign key to the root doc” (like you would do in a RDBMS).
That is, in my example I would create a Participants collection, where
each doc holds the id of a Course. Do you agree?
You can use slice like this:
Also, i guess you know that mongodb always return root document, but you can change content of this documents by specifying fields you want load and size of nested array.