2016-10-20 29 views
0

現在、c#を使用してMongoDbにクエリを実行しているときに、Imに問題が発生しています。問題は、正しい結果または正しい数の結果が返されないということです。私は結果の正確な数を知らないが、それは100未満でなければならない。代わりに、私は350k-500kの結果を受け取っています(多くはnullです)。もう1つの問題は、プログラムが処理を完了するまでに10分以上かかることです。C#のクエリでMongoDBが正しい結果を返さない

次のコードの問題の一部を見ることができます:

public List<BsonDocument> find_All_Documents_With_pIDs() 
    {    
     List<string> databases = new List<string>(); 
     List<BsonDocument> pDocs = new List<BsonDocument>(); 
     databases.AddRange(mongo_Server.GetDatabaseNames()); 

     //iterate through each db in mongo 
     foreach (string dbName in databases) 
     { 
      List<string> collections = new List<string>(); 
      var database = mongo_Server.GetDatabase(dbName); 
      collections.AddRange(database.GetCollectionNames()); 

      //iterate through each collection 
      foreach (string colName in collections) 
      { 
       var collection = database.GetCollection(colName); 

       //Iterate through each document 
       foreach (var document in collection.FindAllAs<BsonDocument>()) 
       { 
        //Get all documents that have a pID in either the main document or its sub document      
        IMongoQuery query = Query.Exists(document.GetElement("_id").ToString().Remove(0,4) + ".pID"); 
        IMongoQuery subQuery = Query.Exists(document.GetElement("_id").ToString() + ".SubDocument.pID"); 
        pDocs.AddRange(collection.Find(query)); 
        pDocs.AddRange(collection.Find(subQuery)); 
       } 
      } 
     } 

     //Theres a collection used earlier in the program to backup the documents before processing. Not removing the documents from the list found in this location will result in duplicates. 
     return remove_Backup_Documents_From_List(pIDs); 
    } 

すべてのヘルプは歓迎です!

編集:

以下は受信したデータの画面キャプチャです。いないすべてのデータは、以下のようにnullであるが、非常に大きな金額は次のとおりです。

enter image description here

+0

あなたは何をしようとしていますか?説明していただけますか? – Saleem

+0

この質問を使用して、文書またはそのサブ文書にpIDを持つすべての文書を取得しようとしています。サブ文書の名前は変わることはありませんが、文書の中にはサブ文書がありません。なぜ2つのクエリが必要なのでしょうか。 – CodePull

答えて

1

あなたのスクリプトは、最初のデータベース

collection.FindAllAs<BsonDocument>() 

からすべてのドキュメントを持参して、それぞれのクエリを組み立てています1。これは、おそらくクエリが遅い理由です。

foreach (string colName in collections) 
{ 
    var collection = database.GetCollection(colName); 

    //Query for all documents that have pID 
    IMongoQuery query = Query.And([Query.Exists("pID"), // The field exists 
     Query.NE("pID", BsonNull.Value), //It is not "null" 
     Query.NE("pID", BsonString.Null)]); //It is not empty i.e. = "" 

    //Query for all documents that have Subdocument.pID 
    IMongoQuery subQuery = Query.And([Query.Exists("SubDocument.pID"), // The field exists 
     Query.NE("SubDocument.pID", BsonNull.Value), //It is not "null" 
     Query.NE("SubDocument.pID", BsonString.Null)]); //It is not empty i.e. = "" 


    IMongoQuery totalQuery = Query.Or([query, subQuery]); 


    List<BsonDocument> results = collection.Find(totalQuery); 
    if (results.Count > 0) { 
     pDocs.AddRange(results); //Only add to pDocs if query returned at least one result 
    } 
} 

設定したいずれかのpIDまたはSubdocument.pIDフィールドを持つ文書のみを返すクエリを組み立てるその方法:あなたが次のことを行うことができます別の方法として

+0

良いニュースは、あなたの編集を試みたときに、処理時間が1分に短縮されたことです!欠点は、まだ420000件の結果が返っていることです。結果の大部分は、各要素のヌル名と値を保持します。いずれの文書の要素もnullではありません。 – CodePull

+0

私は、クエリごとに新しいリストを作成することで、ナル値なしで数字を3412にすることができました。 – CodePull

+0

By "結果の大部分は各要素のヌル名と値を保持しています。いずれの文書の要素もnullではありません"フィールドpIDが_null_または空に設定された文書文字列 '' "'? もしそうなら、これらのケースも検証するためにクエリを編集しました。 – fmello

関連する問題