Delete Documents from CosmosDB based on condition through Query Explorer

后端 未结 4 1306
天涯浪人
天涯浪人 2021-02-18 14:56

What\'s the query or some other quick way to delete all the documents matching the where condition in a collection?
I want something like DELETE * FROM c WHE

4条回答
  •  自闭症患者
    2021-02-18 15:44

    I wrote a script to list all the documents and delete all the documents, it can be modified to delete the selected documents as well.

    var docdb = require("documentdb");
    var async = require("async");
    
    var config = {
      host: "https://xxxx.documents.azure.com:443/",
      auth: {
        masterKey: "xxxx"
      }
    };
    
    var client = new docdb.DocumentClient(config.host, config.auth);
    
    var messagesLink = docdb.UriFactory.createDocumentCollectionUri("xxxx", "xxxx");
    
    var listAll = function(callback) {
      var spec = {
        query: "SELECT * FROM c",
        parameters: []
      };
    
      client.queryDocuments(messagesLink, spec).toArray((err, results) => {
        callback(err, results);
      });
    };
    
    var deleteAll = function() {
      listAll((err, results) => {
        if (err) {
          console.log(err);
        } else {
          async.forEach(results, (message, next) => {
            client.deleteDocument(message._self, err => {
              if (err) {
                console.log(err);
                next(err);
              } else {
                next();
              }
            });
          });
        }
      });
    };
    
    var task = process.argv[2];
    switch (task) {
      case "listAll":
        listAll((err, results) => {
          if (err) {
            console.error(err);
          } else {
            console.log(results);
          }
        });
        break;
      case "deleteAll":
        deleteAll();
        break;
    
      default:
        console.log("Commands:");
        console.log("listAll deleteAll");
        break;
    }
    

提交回复
热议问题