2017-03-17 76 views
0

有沒有辦法使用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

回答

0

嘗試一些像:

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

'System.InvalidOperationException:無法確定對於x => x.Bars.Select(Y => y.Baz).' – Mardoxx

+0

那奇怪...序列化信息我試圖按原樣運行它: 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

+0

你使用的是什麼版本的mongodb驅動,我的似乎不喜歡linq select語句 – Mardoxx