-
Type: Bug
-
Resolution: Works as Designed
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.14.1
-
Component/s: Linq
-
None
Summary
Running MongoDB locally, using driver 2.14.1
Using LINQ (either V2 or V3), when doing an unwind and returning a field that is missing but that has a default value, an error "No matching creator found" is thrown. I also tried making the property for the missing field nullable ("int?"), same problem.
How to Reproduce
Paste the code below into a new console application and run it. The command "var m2 ..." will throw an error. I tried .NET 5.0 and .NET Code 3.1, both exhibit the problem.
Also tried the following things (with the same result):
- remove the BsonDefaultValue attribute, use nullable type int? instead
- remove the BsonDefaultValue attribute, create a parameter-less constructor that sets Runtime = 0
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 System; using System.Collections.Generic; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; using MongoDB.Driver; using MongoDB.Driver.Linq;namespace BugReport { class Program { class Movie { [BsonElement("id"), BsonId] public ObjectId Id { get; set; } [BsonElement("title")] public string Title { get; set; } [BsonElement("directors")] public List<string> Directors { get; set; } [BsonElement("runtime")] [BsonDefaultValue(0)] public int Runtime { get; set; } } static void Main(string[] args) { var connectionString = "mongodb://localhost"; var clientSettings = MongoClientSettings.FromConnectionString(connectionString); clientSettings.LinqProvider = LinqProvider.V2; var mongoClient = new MongoClient(clientSettings); var db = mongoClient.GetDatabase("bug_report_jla"); var moviesBson = db.GetCollection<BsonDocument>("movies"); moviesBson.DeleteMany(new BsonDocument()); moviesBson.InsertOne(new BsonDocument { { "title", "Hannibal" }, { "directors", new BsonArray { "Ridley Scott" } } }); Console.WriteLine(moviesBson.AsQueryable().ToList().Count); var moviesLinq = db.GetCollection<Movie>("movies"); var m1 = moviesLinq.AsQueryable() .SelectMany(m => m.Directors, (m, d) => new { Director = d, Title = m.Title }) .Where(m => m.Director == "Ridley Scott") .ToList(); var m2 = moviesLinq.AsQueryable() .SelectMany(m => m.Directors, (m, d) => new { Director = d, Title = m.Title, Runtime = m.Runtime }) .Where(m => m.Director == "Ridley Scott") .ToList(); ; Console.ReadKey(); } } }
Additional Background
I stepped through the debugger code and it appears that <string, string, int> is not considered a suitable creator for <string, string>. I'm new to this code though, so I'm not sure if this makes any sense.