MongoDB驱动程序C#强类型嵌套索引
问题描述:
有没有办法使用C#驱动程序创建强类型嵌套索引。MongoDB驱动程序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)));
这工作,但增加了“巴兹”
collection.Indexes.CreateOne(Builders<Foo>.IndexKeys.Ascending(x => x.Bars.First().Baz));
他们没有要添加“Bars.Baz”
MongoDB的驱动程序版本的索引的索引是2.4.3.23
答
尝试一些像:
var collection = database.GetCollection<Foo>("foos");
collection.Indexes.CreateOne(Builder<Foo>.IndexKeys.Ascending(f=>
f.Bars.Select(str=> str.Baz)));
'System.InvalidOperationException:无法确定对于x => x.Bars.Select(Y => y.Baz).' – Mardoxx
那奇怪...序列化信息我试图按原样运行它: var collection = _FilesDb.GetCollection(“foos”); collection.Indexes.CreateOneAsync(Builders .IndexKeys.Ascending(x => x.Bars.Select(y => y.Baz))); 它创造了钥匙。 我检查YOUE错误,找到这个: http://stackoverflow.com/questions/28039274/mongodb-unable-to-determine-the-serialization-information-for-the-expression-err 我猜你的真实对象有一个isuue ...试图检查了这一点。 –
AvrahamL
你使用的是什么版本的mongodb驱动,我的似乎不喜欢linq select语句 – Mardoxx