2013-02-15 7 views
7

में एक कंपाउंड इंडेक्स बनाना मैं एक कंपाउंड इंडेक्स बनाना चाहता हूं जहां एक कुंजी आरोही में होनी चाहिए, अवरोही क्रम में दूसरी कुंजी।सी #

मैं यह कैसे कर सकता हूं?

मेरे पास एक स्ट्रिंग है जिसमें उपयोगकर्ता द्वारा चयनित संपत्ति नाम शामिल हैं।

collection.EnsureIndex(IndexKeys.Descending(selectedProperties[0]), 
       IndexKeys.Ascending(selectedProperties[1])), 
IndexOptions....... 

काम नहीं करता है

उत्तर

11

यहाँ एक तरीका है:

var keys = new IndexKeysBuilder(); 

keys.Ascending(keyName1); 
keys.Descending(keyName2); 

var options = new IndexOptionsBuilder(); 
options.SetSparse(true); 
options.SetUnique(false); 
collection.EnsureIndex(keys, options); 
+0

इस उत्तर के लिए धन्यवाद। – user2010435

+1

@ user2010435 - अगर यह आपके लिए काम करता है, तो मैं इसे स्वीकार करने के रूप में चिह्नित करने का सुझाव दूंगा। – WiredPrairie

5

EnsureIndex अब अप्रचलित है, इसलिए CreateIndex बजाय प्रयोग किया जाना चाहिए। स्थिर बिल्डर वर्ग भी समर्थन धाराप्रवाह उपयोग हैं: IndexKeys और IndexOptions

तो साफ विकल्प है:

collection.CreateIndex(
    IndexKeys.Ascending(keyName1).Descending(keyName2), 
    IndexOptions.SetSparse(true).SetUnique(false)); 

और एक सामान्य प्रकार सुरक्षित विकल्प के लिए:

collection.CreateIndex(
    IndexKeys<Document>.Ascending(d => d.Property1).Descending(d => d.Property2), 
    IndexOptions.SetSparse(true).SetUnique(false)); 
3

चालक की v2.x में वे पूरी तरह से एपीआई के लिए इतना वर्तमान में जिस तरह से बदल दिया है एक यौगिक सूचकांक एसिंक्रोनस रूप से बनाने के लिए (जो पसंद है) है:

await collection.Indexes.CreateOneAsync(
    Builders<Hamster>.IndexKeys.Ascending(_ => _.Name).Descending(_ => _.Age), 
    new CreateIndexOptions { Background = true }); 

है और तालमेल:

collection.Indexes.CreateOne(
    Builders<Hamster>.IndexKeys.Ascending(_ => _.Name).Descending(_ => _.Age), 
    new CreateIndexOptions { Sparse = true }); 
+0

v2.3 (इंडेक्स.क्रेटमैनी()) में एक से अधिक अनुक्रमणिका कैसे बनाएं? – Sri