class Test
{
public ObjectId Id { get; set; }
public List<OtherObject> OtherObjects { get; set; }
}
class OtherObjects
{
public string Name { get; set; }
public string Value { get; set; }
}
I need to create an index in Name and Value, since i’ll be querying on those fields. Using driver i’m doing this:
var index = new CreateIndexModel<Test>(Builders<Test>.IndexKeys
.Ascending("OtherObjects.Name")
.Ascending("OtherObjects.Value")
);
It would be great to have support for that using OwnsMany for model builder, something like this:
Do you think this is feasible to implement? For us it’s the last usage of Mongo.Driver for creating indexes, everything else we can do using the HasIndex function
I’m now trying to set other properties like HasBsonRepresentation on nested element and i think this has exactly same issue. EF is recommending OwnsMany/OwnsOne apis for that
You’re right in that there is HasIndex fluent API support in EF - I was looking for it in the wrong place.
I’ve not been able to repro your issue with HasBsonRepresentation though - here’s what I’m doing. If this doesn’t work for you can you provide a small snippet that fails? Thanks!
Yea, i’m sorry, the issue was on the other line. I think this is a bug, or maybe i’m doing something wrong:
var options = new CreateIndexOptions<ChartOfAccounts>
{
Unique = true,
// this filter will only index documents that have a string value for the Version1Id field. So it will ignore null values, which is what we want.
PartialFilterExpression = Builders<ChartOfAccounts>.Filter.Type(n => n.Version1Id, BsonType.String)
};
builder.HasIndex(n => n.Version1Id)
.HasCreateIndexOptions(options);
This filter works correctly on Driver, EF throws exception:
System.InvalidCastException: Unable to cast object of type 'MongoDB.Driver.CreateIndexOptions`1[GeneralLedger.Microservice.Data.Entities.ChartOfAccounts]' to type 'MongoDB.Driver.CreateIndexOptions`1[MongoDB.Bson.BsonDocument]'.
2025-01-22T16:06:38 at MongoDB.Driver.CreateIndexOptions`1.CoercedFrom(CreateIndexOptions options)
2025-01-22T16:06:38 at MongoDB.Driver.CreateIndexModel`1..ctor(IndexKeysDefinition`1 keys, CreateIndexOptions options)
2025-01-22T16:06:38 at MongoDB.EntityFrameworkCore.Storage.MongoClientWrapper.CreateIndexDocument(IIndex index, String indexName)
2025-01-22T16:06:38 at MongoDB.EntityFrameworkCore.Storage.MongoClientWrapper.CreateIndexesAsync(IEntityType entityType, CancellationToken cancellationToken)
2025-01-22T16:06:38 at MongoDB.EntityFrameworkCore.Storage.MongoClientWrapper.CreateDatabaseAsync(IDesignTimeModel model, CancellationToken cancellationToken)
Our EF indexes expect the CreateIndexOptions to be of type BsonDocument, for example:
var options = new CreateIndexOptions<BsonDocument> {
Unique = true,
PartialFilterExpression = Builders<BsonDocument>.Filter.Type(n => n["Version1Id", BsonType.String)
};
This is a known limitation caused by the underlying MongoDB C# Driver not being able to give us MQL for a given LINQ statement yet. We’re hoping to address that in a future release as we’ll also need it for the EF9 bulk update operations.
The BsonDocument equivalent above should work for both although obviously it is more error prone given the lack of lambdas and manually specified element names.