Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1955

Add a type safe way to use the IndexKeys builder to create an index on a nested field of an array of documents

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.19.0
    • Affects Version/s: 2.4.3
    • Component/s: API
    • Labels:
    • Environment:
      Net452
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      using MongoDB.Bson.Serialization.Attributes;
      using MongoDB.Driver;
      using System.Collections.Generic;
      using System.Linq;
      using Xunit;
      
      namespace IndexTest
      {
          [BsonIgnoreExtraElements]
          public class Foo
          {
              public Foo() { }
      
              public List<Bar> Bars { get; set; }
          }
      
          [BsonIgnoreExtraElements]
          public class Bar
          {
              public Bar() { }
      
              public string Baz { get; set; }
          }
      
          public class TestClass
          {
              [Fact]
              public void Test()
              {
                  var client = new MongoClient("mongodb://localhost:27017");
                  var database = client.GetDatabase("test");
                  var collection = database.GetCollection<Foo>("foos");
      
                  // Invalid
                  // { "Baz" : 1 }
                  collection.Indexes.CreateOne(Builders<Foo>.IndexKeys.Ascending(x => x.Bars.First().Baz));
      
                  //Invalid
                  // { "Bar.0.Baz" : 1 }
                  collection.Indexes.CreateOne(Builders<Foo>.IndexKeys.Ascending(x => x.Bars[0].Baz));
      
                  // Valid but relies on magic string
                  // { "Bar.Baz" : 1 }
                  collection.Indexes.CreateOne("{\"Bar.Baz\" : 1}");
      
                  // Failure
                  // System.InvalidOperationException : Unable to determine the serialization information for x => x.Bars.Select(y => y.Baz).
                  collection.Indexes.CreateOne(Builders<Foo>.IndexKeys.Ascending(x => x.Bars.Select(y => y.Baz)));
              }
          }
      }
      
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            Mardoxx Mardoxx
            Votes:
            8 Vote for this issue
            Watchers:
            14 Start watching this issue

              Created:
              Updated:
              Resolved: