Question

I have a C# class which has an id field of type ObjectId:

[BsonId]
[SolrUniqueKey("id")]
public ObjectId Id { get; set; }

In Solr, the schema has defined id as:

 <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 

When attempting to pull an object from Solr it fails because it can't cast the string id to MongoDB ObjectId. Does anyone an easy was of fixing this without changing the source for SolrNet?

Was it helpful?

Solution

For anyone interested, below is how I ended-up doing it without modifying SolrNet source code.

I've added an additional property in the class called IdAsString and set it as "BsonIgnore" so that it doesn't get stored in MongoDB, however I use that field to map it to Solr's id field:

[BsonId]
public ObjectId Id { get; set; }

[BsonIgnore]
[SolrUniqueKey("id")]
public string IdAsString
{
  get { return Id.ToString(); }
  set { Id = ObjectId.Parse(value); }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top