2011-09-07 25 views
0

我通過C#驅動程序將.NET類的實例存儲在MongoDB集合中。該類包含一個數組 - 例如 - MyArray。是否有可能使用MongoDB C#驅動程序查詢某個字符串(元素)在數組中的位置?實際的例子是我想找到集合中其MyArray屬性在位置0上具有特定字符串的文檔。是否有可能通過MongoDB C#查詢獲取元素在數組中的位置?

非常感謝。

.NET類:

namespace TestClasses 
{ 
    public class TestClass 
    { 
     public TestClass() { 

     } 

     public string[] MyArray 
     { 
      get; 
      set; 
     } 

     protected string Name 
     { 
      get; 
      set; 
     } 

    } 
} 

回答

1

是,在查詢中使用點符號(其中searchValue是你正在尋找的值):

MongoServer server = MongoServer.Create(host); 
MongoDatabase db = server.GetDatabase(databaseName); 
MongoCollection Collection = db.GetCollection("collection"); 

var query = new QueryDocument("MyArray.0", searchValue); 
var testCollection = matchCollection.FindAs<TestClass>(query); 
+0

非常感謝Jeffsaracco.Now我有一個更復雜的情況。現在我有另一個數組 - MyArray2。我想要做的是首先查找其MyArray包含給定字符串String1的文檔,然後在MyArray中查找給定字符串的位置,並過濾出其MyArray2的相同位置上的元素是給定字符串String2的文檔。假設MyArray和MyArray2是字符串數組,並且沒有重複的元素。可能嗎? – flyerno1

+0

我認爲做到這一點的唯一方法是通過做一些map/reduce函數,在該函數中使用javascript處理數組,或者通過執行查詢如 {「MyArray1」:{$ exists:true},「MyArray2」:{$ exists:true}} 然後獲取文檔並在服務器端處理它們。我不認爲MongoDB有任何類型的運營商爲您尋找什麼 – jeffsaracco

+0

是的,我完全是這樣做的。我將文檔取回並在服務器端處理它們。謝謝。 – flyerno1

相關問題