简体   繁体   English

处理409冲突文档Node.js Nano CouchDB

[英]Handle 409 conflict document nodejs nano couchdb

I've been doing some research on this matter, that couchdb gives too many document conflicts . 我一直在做一些有关此问题的研究,因为ouchdb提供了太多的文件冲突

I've seen a solution Updating a CouchDB document in nano saying this: 我看过一个解决方案,用nano更新CouchDB文档,这是这样的:

  1. Get document 取得文件
  2. Save the _rev 保存_rev
  3. Apply changes 应用更改
  4. Try to send updated document with saved _rev 尝试使用保存的_rev发送更新的文档
  5. Go to step 1 in case of a 409 如果是409,请转到步骤1

And I've tried to produce a function that does it, until the last step: 我试图产生一个执行此功能的函数,直到最后一步:

nano_update = function ( data, id, callback ) {

    var db = this;

    db.get( id, function( err, doc ) { //Get _rev of the document

        if ( !err ) {
            for ( var k in data ) {
                //Replace defined information while keeping the other
                if ( k.toLowerCase() !== '_rev' ) //Do not override _rev
                    doc[ k ] = data[ k ];
            }

        return db.insert( doc, id, callback ); //Insert with _rev to update
        }
    });
}

//Call it
var database = nano.use( 'databaseName' );
nano_update.call( database, { counter: Math.random() }, 'documentName' );

This is close to what I expect, however, if I open 2 windows making the same request continuously, it will reach a point where the 2 documents conflict making my server crash. 这接近于我的预期,但是,如果我连续打开2个发出相同请求的窗口,它将到达2个文档冲突而导致服务器崩溃的地步。

So, by saying '5. 因此,说出“ 5。 Go to step 1 incase of a 409' , does it mean I have to do a recursion of calling the same function? 如果是409' ,请转到步骤1 ,这是否意味着我必须递归调用相同的函数? If the documents continue to conflict, it's going to be an infinite loop for sure, and my server will just crash again. 如果文档继续冲突,那么肯定会是一个无限循环,而我的服务器将再次崩溃。 Currently, what I am doing is to NOT allow more than 1 request per 5 seconds, but that won't do well in the future. 当前,我正在做的是每5秒不允许超过1个请求,但是将来这样做不会很好。

How can I correctly handle 409 conflicting CouchDB documents using nano in nodejs? 如何在Node.js中使用Nano正确处理409个有冲突的CouchDB文档?

The reason you get 409 is because the _rev is outdated, as every document update changes it. 出现409的原因是因为_rev已过时,因为每个文档更新都会对其进行更改。 So don't hold on to the _rev . 因此,不要坚持使用_rev

If you need to update multiple documents, try using the Bulk Document API , or you can use Partial Update , which kind of like a hack for you to update a document without specifying the _rev (it automatically read the _rev from the db). 如果您需要更新多个文档,请尝试使用Bulk Document API ,也可以使用Partial Update ,它就像是一种hack,可用于在不指定_rev情况下更新文档(它会自动从db中读取_rev )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM