Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoDB C# Driver: How do I ensure an index using LINQ expressions on the contents of an array?
    primarykey
    data
    text
    <p>How can I ensure an index using LINQ expressions on the contents of an array using the MongoDB C# driver?</p> <p>I currently have a domain object that looks roughly like this:</p> <pre><code>public class Team { public Team() { Members = new List&lt;LazyReference&gt;(); } public MongoDB.Bson.ObjectId Id { get; set; } public string DisplayName { get; set; } public LazyReference Leader { get; set; } public List&lt;LazyReference&gt; Members { get; private set; } } public class LazyReference { public MongoDB.Bson.ObjectId Id { get; set; } } </code></pre> <p>On my collection, I have initialized a couple of indexes like this:</p> <pre><code>collection.EnsureIndex(IndexKeys&lt;Team&gt;.Ascending(t =&gt; t.DisplayName), IndexOptions.SetUnique(true)); collection.EnsureIndex(IndexKeys&lt;Team&gt;.Ascending(t =&gt; t.Leader.Id), IndexOptions.SetUnique(false)); </code></pre> <p>How can I do the same to ensure a fast index of the Members when looking up by Members[n].Id, where n is any of the members? I am aware that I can create an index as follows, but since this does not use the LINQ expressions, it is not safe for future renames of properties.</p> <pre><code>collection.EnsureIndex(new IndexKeysBuilder().Ascending("Members._id"), IndexOptions.SetUnique(false)); </code></pre> <p>I have also tried the following, but it only indexes the index specified:</p> <pre><code>collection.EnsureIndex(IndexKeys&lt;Team&gt;.Ascending(t =&gt; t.Members[0].Id), IndexOptions.SetUnique(false)); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload