2016-10-20 3 views
0

C#을 사용하여 MongoDb를 쿼리 할 때 현재 Im에 문제가 있습니다. 문제는 올바른 결과 또는 올바른 수의 결과가 반환되지 않는다는 것입니다. 결과의 정확한 수를 모르지만 100 미만이어야합니다. 대신, 나는 약 350k-500k 결과를 받는다 (많은 것은 null이다). 다른 문제는 프로그램이 처리를 끝내기까지 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 :

스크립트가 먼저 데이터베이스

collection.FindAllAs<BsonDocument>() 

을에서 모든 문서를 가져오고 각에 대한 쿼리를 조립한다

enter image description here

+0

무엇을하려고합니까? 설명해 주시겠습니까? – Saleem

+0

이 쿼리를 사용하여 문서 또는 해당 하위 문서에 pID가있는 모든 문서를 가져 오려고합니다. 하위 문서의 이름은 절대로 변경되지 않지만 일부 문서에서는 하위 문서가 없으므로 두 개의 쿼리가 필요한 이유는 무엇입니까? – CodePull

답변

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

각 쿼리에 대해 새 목록을 만들어 null이없는 3412로 번호를 가져올 수있었습니다. – CodePull

+0

"대다수의 결과는 각 요소에 대해 null 이름과 값을 보유합니다. 어떤 문서의 요소도 null이 아닙니다."pID "필드가 _null_ 또는 비어있는 문서가 있음을 의미합니까? 문자열'' "'? 그렇다면이 사례의 유효성을 검사하기 위해 검색어를 수정했습니다. – fmello

관련 문제