I’m trying to use MongoDB to cache some data for me, but I can’t seem to get the code to return a specific collection. I can get other collections without an issue, but for this one cannot get it to work and I have no idea why. The only error that I get is:
Ambiguous discriminator
'revamp@904'
I have searched long and hard for any indication as to what this means. Here’s the code I’m using:
let GetCacheDataObject ctxName cacheName collection =
let ctx = new DataContext(ctxName)
let q = Query.EQ("CacheName", BsonValue.Create(cacheName.ToString()))
let entity =
match ctx.Db.CollectionExists(collection) with
| false -> null
| _ -> ctx.Db.GetCollection(typeof<DataObject>, collection).FindOneAs<DataObject>(q)
entity
Once it his the ‘FindAsOne’, this error gets thrown.
The DataObject is a very basic custom object to hold data. Here’s the definition:
public class DataObject:IHaveIdentifier
{
public BsonObjectId _id { get; set; }
//public long Id { get; set; }
public string[] Columns { get; set; }
public IEnumerable<object[]> Rows { get; set; }
public string CacheName { get; set; }
public int GetColumnIndex(string column)
{
for (int i = 0; i < this.Columns.Length; i++)
if (this.Columns[i] == column)
return i;
return -1;
}
}
And the IHaveIdentifier interface is pretty basic:
public interface IHaveIdentifier
{
BsonObjectId _id { get; set; }
}
Here’s the code that was used to save the data in the first place:
member x.Save<'T when 'T :> IHaveIdentifier>(entity:'T, collection:string) =
if (entity._id = null ) then x.Insert<'T>(entity, collection)
else
x.Delete(entity, collection)
x.Insert<'T>(entity, collection)
x.VerifyNoErrors()
let CacheDataObject<'T when 'T :> IHaveIdentifier>(entity:'T, ctxName, collection) =
let ctx = new DataContext(ctxName)
ctx.Save(entity, collection)
This code was working the other day then something changed and I cannot seem to figure out what is going on.
UPDATE: Added initial saving code above
Brian and Robert,
Your input helped me figure out exactly what was going on. I was able to determine that the IEnumberable Rows property was exactly the issue. I was setting the Rows to a seq. In F# Seq are lazily evaluated, which in this case meant that Rows was being set to the output value (kind of like a ref) of the function, not the actual result. Once I converted the Rows using Seq.ToArray, then everything worked as expected.
Thanks you both so much for pointing me down the right path.