【问题标题】:MongoDB Driver C# Strongly Typed Nested IndexMongoDB 驱动程序 C# 强类型嵌套索引
【发布时间】:2017-08-08 21:49:17
【问题描述】:

有没有办法使用 C# 驱动程序创建强类型嵌套索引。

我想要这种索引:db.foos.ensureIndex({'Bars.Baz': 1});

public class Foo {
    public Something() { }

    public List<Bar> Bars { get;set; }
}

public class Bar {
    public string Baz { get; set; }
}

var collection = database.GetCollection<Foo>("foos");
collection.Indexes.CreateOne(Builder<Foo>.IndexKeys.Ascending(/*What goes here?*/));

以下工作但创建“Bars.0.Baz”的索引:

collection.Indexes.CreateOne(Builders<Foo>.IndexKeys.Ascending(x => x.Bars[0].Baz));

这根本不起作用,并出现序列化错误

collection.Indexes.CreateOne(Builders<Foo>.IndexKeys.Ascending(x => x.Bars.Select(y => y.Baz)));

这可行,但添加了“Baz”的索引

collection.Indexes.CreateOne(Builders<Foo>.IndexKeys.Ascending(x => x.Bars.First().Baz));

他们都不想添加“Bars.Baz”的索引

MongoDB驱动版本是2.4.3.23

【问题讨论】:

    标签: c# mongodb mongodb-.net-driver


    【解决方案1】:

    尝试类似:

    var collection = database.GetCollection<Foo>("foos");
    collection.Indexes.CreateOne(Builder<Foo>.IndexKeys.Ascending(f=> 
                                                  f.Bars.Select(str=> str.Baz)));
    

    【讨论】:

    • System.InvalidOperationException : Unable to determine the serialization information for x =&gt; x.Bars.Select(y =&gt; y.Baz).
    • 这很奇怪...我尝试按原样运行它: var collection = _FilesDb.GetCollection("foos"); collection.Indexes.CreateOneAsync(Builders.IndexKeys.Ascending(x => x.Bars.Select(y => y.Baz)));它创造了钥匙。我检查了你的错误并发现:stackoverflow.com/questions/28039274/… 我猜你的 REAL 对象有问题...尝试检查一下。
    • 你用的是什么版本的mongodb驱动,我的好像不太喜欢linq select语句
    • 我还在使用之前的版本——2.1.0。您对 linq 查询有一般问题,还是仅针对此用例?
    • 似乎只是针对这个用例,我已经在他们的 JIRA 上打开了一张支持票,但目前还没有回复
    猜你喜欢
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 2017-05-09
    • 2016-11-16
    • 1970-01-01
    • 2018-12-17
    • 2018-11-16
    • 1970-01-01
    相关资源
    最近更新 更多