繁体   English   中英

从F#中的MongoDB获取错误“Ambiguous discriminator'revamp @ 904”(使用C#lib)

[英]Getting an error “Ambiguous discriminator 'revamp@904” from MongoDB in F# (using C# lib)

我正在尝试使用MongoDB为我缓存一些数据,但我似乎无法获取返回特定集合的代码。 我可以在没有问题的情况下获得其他收藏品,但是对于这个,它无法让它工作,我不知道为什么。 我得到的唯一错误是:

模棱两可的歧视者'revamp@904'

我已经搜索了很长时间并且很难找出这意味着什么。 这是我正在使用的代码:

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

一旦它成为'FindAsOne',就会抛出这个错误。

DataObject是一个非常基本的自定义对象来保存数据。 这是定义:

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;
    }
}

而IHaveIdentifier接口非常基本:

public interface IHaveIdentifier
{
    BsonObjectId _id { get; set; }
}

这是用于首先保存数据的代码:

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)

这段代码前几天工作,然后发生了一些变化,我似乎无法弄清楚发生了什么。

更新:添加了上面的初始保存代码

您可以在mongo shell中运行等效查询,并查看现有文档的外观。

我怀疑它有一个“_t”值导致问题。 如果是这样,问题不在于FindOne,而在于首先保存文档的方式,因此我们可能需要再往后看看。

布莱恩和罗伯特,你的意见帮助我弄明白究竟发生了什么。 我能够确定IEnumberable Rows属性正是问题所在。 我把行设置为seq。 在F#Seq中进行了延迟评估,在这种情况下,Rows被设置为函数的输出值(类似于ref),而不是实际结果。 一旦我使用Seq.ToArray转换了行,那么一切都按预期工作。

非常感谢你们两位指导我走正确的道路。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM