Azure DocumentDB不支持UPSERT。是否有合理的工作来实现相同的功能?
使用存储过程检查文档是否存在以确定是否应该执行插入或更新是否有效策略?
如果我需要批量执行数千个这样的工作怎么办?
投票支持此功能:
http://feedback.azure.com/forums/263030-documentdb/suggestions/7075256-provide-for-upsert
更新 - 这是我尝试批量upsert存储过程。
function bulkImport(docs) {
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var count = 0;
if (!docs) throw new Error('Docs parameter is null');
var docsLength = docs.length;
if (docsLength == 0) {
getContext().getResponse().setBody(0);
}
tryUpsert(docs[count], callback);
function tryUpsert(doc, callback) {
var query = { query: ""select * from root r where r.id = @id"", parameters: [ {name: ""@id"", value: doc.id}]};
var isAccepted = collection.queryDocuments(collectionLink, query, function(err, resources, options) {
if (err) throw err;
if(resources.length > 0) {
// Perform a replace
var isAccepted = collection.replaceDocument(resources[0]._self, doc, callback);
if (!isAccepted) getContext().getResponse().setBody(count);
}
else {
// Perform a create
var isAccepted = collection.createDocument(collectionLink, doc, callback);
if (!isAccepted) getContext().getResponse().setBody(count);
}
});
if (!isAccepted) getContext().getResponse().setBody(count);
}
function callback(err, doc, options) {
if (err) throw err;
// One more document has been inserted, increment the count.
count++;
if (count >= docsLength) {
// If we have created all documents, we are done. Just set the response.
getContext().getResponse().setBody(count);
} else {
// Create next document.
tryUpsert(docs[count], callback);
}
}
}
答案 0 :(得分:3)
更新(2015-10-06): Atomic upsert。
是的,商店程序适用于upsert。
DocumentDB的Github上甚至提供了代码示例:
Upsert(针对插入进行了优化):https://github.com/aliuy/azure-node-samples/blob/master/documentdb-server-side-js/stored-procedures/upsert.js
Upsert(针对替换进行了优化):https://github.com/aliuy/azure-node-samples/blob/master/documentdb-server-side-js/stored-procedures/upsertOptimizedForReplace.js
批量导入/上传: https://github.com/Azure/azure-documentdb-hadoop/blob/master/src/BulkImportScript.js