如果多个列相似(posts_by和location),我有一个文档,我想转向is_approved = false。
我无法编写可以获得多个重复列的查询。
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");
}
}
在上面的代码中,在函数'findDuplicates'中,我无法编写正确的查询,通过它我可以获得所有重复的记录。
我试过使用distinct,group by。有。我认为有记录不支持他们
答案 0 :(得分:0)
以下查询可帮助您查找与两个属性值匹配的文档。
from django.db import IntegrityError
from rest_framework import status
from rest_framework.generics import ListCreateAPIView
from rest_framework.response import Response
class MyListCreateAPIView(ListCreateAPIView):
def create(self, request, *args, **kwargs):
try:
return super(ListCreateAPIView, self).create(request, *args, **kwargs)
except IntegrityError:
content = {'error': 'IntegrityError'}
return Response(content, status=status.HTTP_400_BAD_REQUEST)