2016-07-12 3 views
0

Ich habe ein Dokument, wo ich drehen von is_approved = false, wenn mehrere Spalten ähnlich sind (posted_by und Ort).DocumentDB: Problem mit Update-Liste mit mehreren Spalten

Ich bin nicht in der Lage, eine Abfrage zu schreiben, die mehrere doppelte Spalten erhalten können ..

function Prop() { 
    var collection = getContext().getCollection(); 
    var collectionLink = collection.getSelfLink(); 
    var response = getContext().getResponse(); 
    var counter = 0; 
    var responseBody = { 
     updated: 0, 
     continuation: true, 
     error: "", 
     log: "" 
    }; 

    // Validate input. 
    getFullListOfPosts(); 

    // Recursively queries for a document by id w/ support for continuation tokens. 
    // Calls findDuplicates(document) as soon as the query returns a document. 
    function getFullListOfPosts(continuation) { 
     var query = { 
      query: "select * from root r ORDER BY r.created.epoch DESC" 
     }; 

     var requestOptions = { 
      continuation: continuation 
     }; 

     var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, proccessFullListOfPosts); 

     // If we hit execution bounds - throw an exception. 
     if (!isAccepted) { 
      responseBody.log += "Query not accepted"; 
      response.setBody(responseBody); 
     } 
    } 

    function proccessFullListOfPosts(err, documents, responseOptions) { 
     if (err) { 
      responseBody.error = err; 
      throw err; 
     } 

     if (documents.length > 0) { 
      // If documents are found, update them. 
      responseBody.log += "Found documents: " + documents.length; 
      findDuplicates(documents); 
     } 
    } 
    // Updates the supplied document according to the update object passed in to the sproc. 
    function findDuplicates(documents) { 
     if (documents.length > 0) { 
      responseBody.log += "Updating documents " + documents.length; 
      //for (var i = 0; i < documents.length; i++) { 
       var first = documents[0]; 
       //The below query is not right. I want the above query to fetch all records with same posted_by and location and turn its is_approved to false 
       var query = 'select * from root r WHERE r.is_approved = true AND r.posted_by = "' + first.posted_by + '"'; 
       var requestOptions = { 
        etag: first._etag 
       }; 
       var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, identifyRecordsToDisable); 

       // Update the document.      // If we hit execution bounds - throw an exception. 
       if (!isAccepted) { 
        responseBody.log += "Update not accepted"; 
        response.setBody(responseBody); 
       } 
     } else { 
      getFullListOfPosts(); 
     } 
    } 

    function identifyRecordsToDisable(err, documents, responseOptions) { 
     if (err) { 
      responseBody.error = err; 
      throw err; 
     } 

     if (documents.length > 0) { 
      // If documents are found, update them. 

      responseBody.log += "Found documents: " + documents.length; 
      for (var i = 0; i < documents.length; i++) { 
       var j = documents[i]; 
       j.is_approved = false; 
       responseBody.log += "^Updated" + j.id + j.is_approved + "" 
       var requestOptions = { 
        etag: j._etag 
       }; 
       var isAccepted = collection.replaceDocument(j._self, j, requestOptions, onReplaced); 

       responseBody.log += "*" + j.id + j.is_approved + "*" 
      } 
      findDuplicates(documents); 
     } 
    } 

    function onReplaced(err, updatedDocument, responseOptions) { 
     if (err) { 
      responseBody.error = err; 
      //throw err; 
     } 
     counter++; 
     response.setBody("Updated " + counter + " documents"); 
    } 
} 

Im obigen Code in der Funktion ‚findDuplicates‘, ich bin nicht in der Lage, eine korrekte Abfrage, durch die ich schreibe kann alle doppelten Datensätze erhalten.
Ich versuchte mit verschiedenen, Gruppe von. haben. Ich denke, dokumentiert unterstützt sie nicht

+0

Wenn Sie weitere Details haben, * bearbeiten Sie Ihre Frage * - keine zusätzlichen Details in Kommentaren posten. –

+0

Können Sie Beispieldokumente bereitstellen? Auch dein erster Satz ist schwer zu verstehen. Kannst du umschreiben/korrigieren? –

Antwort

0

Die folgende Abfrage sollte Ihnen helfen, Dokumente zu finden, die beide Eigenschaftswerte entsprechen.