如何在文档数据库中上载多个文档(批量) [英] How to Upload multiple Document (bulk) in Document DB
问题描述
我有Documents列表(对象),该对象具有多个文档,即存在Json记录,但是当我尝试上传那束Document(记录)时,它没有上传到文档DB上,但是当我上传Single Document记录时,它成功上传了.
I have Documents list(object) that object has multiple documents i.e. Json records are present but while I try to upload that Bunches of Document(Records) it not upload on document DB but while I upload Single Document records it upload succesfully.
List<MyModelClass> listObj = new List<MyModelClass>();
Document doc = await DocumentClient.CreateDocumentAsync("dbs/" + DocumentDatabase.Id + "/colls/" + DocumentCollection.Id, listObj);
以上代码不起作用.....
above code is not working.....
foreach (var item in listObj )
{
Document doc = await Client.CreateDocumentAsync("dbs/" + DocumentDatabase.Id + "/colls/" + DocumentCollection.Id, item);
}
此代码对我有用.....
This code is working for me.....
Syntax : CreateDocumentAsync(String, Object, RequestOptions, Boolean)
Object :- Document object // I Know it as per syntax it need to be "Document Type".
我想通过其他任何方式一次上传所有文档....
I want any Other way to Upload All Document at Once....
推荐答案
一次不能插入多个文档. CreateDocumentAsync()
的调用仅适用于单个文档.
You cannot insert more than one document at a time. The call to CreateDocumentAsync()
is for single documents only.
要完成此操作,您需要设计某种类型的服务器端存储过程来完成此操作,然后在一次调用中将文档数组传递给该函数.您可能需要查看此答案,以了解其他人如何通过使用服务器端功能(实际上是通过创建一个本地文档数组,然后在其存储过程中遍历该数组.因此,创建这样的内容(摘自该答案):
To accomplish this, you'd need to devise some type of server-side stored procedure to accomplish this, and then pass your document array to the function in a single call. You may want to look at this answer to see how someone else solved this using a server-side function, by essentially creating an array of documents locally, and then walking through the array in their stored procedure. So, creating something like this (as excerpted from that answer):
docObject = {
"items": [{doc}, {doc}, {doc}]
}
并将docObject
传递给您的存储过程.
And passing docObject
to your stored procedure.
最终,您的存储过程仍将进行单个插入调用,每个文档一次.但是...您将不会有多个网络往返.并且插入将是事务性的(如果其中一个插入失败,或者您引发了异常,则其他插入将被回滚).
Ultimately, your stored procedure would still be making individual insert calls, one per document. But... you would not have the multiple network round-trips. And the inserts would be transactional (if one of the inserts failed, or you threw an exception, the other inserts would be rolled back).
这篇关于如何在文档数据库中上载多个文档(批量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!