您可以使用 bulkDocs()方法一次更新PouchDB中的文档数组.为此,您需要创建一个文档数组,其中每个文档包含 _id,_rev 以及要更新的值.
假设数据库已命名本地存储在PouchDB中的 my_database 包含3个文档,即doc1,doc2,doc3,其中包含以下内容.
doc1 = {_id: '001', name: 'Ram', age: 23, Designation: 'Programmer'} doc2 = {_id: '002', name: 'Robert', age: 24, Designation: 'Programmer'} doc3 = {_id: '003', name: 'Rahim', age: 25, Designation: 'Programmer'}
假设我们必须将所有3份文件的年龄值增加2年.为此,首先需要获取 _rev 值.因此,使用以下代码获取这些文档的内容.
//Requiring the package var PouchDB = require('PouchDB'); //Creating the database object var db = new PouchDB('my_database'); //Retrieving all the documents in PouchDB db.allDocs({include_docs: true},function(err, docs) { if (err) { return console.log(err); } else { console.log(docs.rows); } });
将上述代码保存为 bulk_fetch.js .执行时,上面的程序为您提供数据库中文档的_id和_rev值,如下所示.
[ { id: '001', key: '001', value: { rev: '1-1604b0c3ff69dc1e261265fd60808404' } }, { id: '002', key: '002', value: { rev: '1-b5e49db7e984841bf12a13e3ee548125' } }, { id: '003', key: '003', value: { rev: '1-a7b342786ecc707aa91f3b321a177b51' } } ]
现在,您可以使用各自的 _id 和 _rev 值更新文档,如下所示.
//Requiring the package var PouchDB = require('PouchDB'); //Creating the database object var db = new PouchDB('my_databas'); //Preparing the document docs = [{_id : '001', _rev: '1-1604b0c3ff69dc1e261265fd60808404', age : 25, }, {_id : '002', _rev: '1-b5e49db7e984841bf12a13e3ee548125', age : 26, }, {_id : '003', _rev: '1-a7b342786ecc707aa91f3b321a177b51', age : 27 }] //Updating the documents in bulk db.bulkDocs(docs, function(err, response) { if (err) { return console.log(err); } else { console.log("Documents Updated Successfully"); } });
将上述代码保存在名为 Update_All_Document.js 的文件中.打开命令提示符并使用node执行JavaScript文件,如下所示.
C:\PouchDB_Examples >node Update_All_Document.js
这将更新存储在本地存储的名为 my_database 的数据库中的所有文档,并显示以下消息.
Documents Updated Successfully
现在,如果执行 bulk_fetch.js 程序通过将 {include_docs:true} 作为参数添加到 allDocs()函数,在回调之前,您将可以看到更新的文档的值,如下所示.
[ { id: '001', key: '001', value: { rev: '2-77f3a9974dd578d12f3f2a33aae64c8d' }, doc: { age: 25, _id: '001', _rev: '2-77f3a9974dd578d12f3f2a33aae64c8d' } }, { id: '002', key: '002', value: { rev: '2-43966007568ce9567c96422195fcfa0d' }, doc: { age: 26, _id: '002', _rev: '2-43966007568ce9567c96422195fcfa0d' } }, { id: '003', key: '003', value: { rev: '2-6c5349652527f4f39583ff14f23cd677' }, doc: { age: 27, _id: '003', _rev: '2-6c5349652527f4f39583ff14f23cd677' } } ]
您可以更新远程存储在服务器(CouchDB)上的数据库中的所有文档.
为此,您需要将路径传递给CouchDB中的数据库,而不是数据库名称,CouchDB中包含要读取的文档.
假设存在数据库在CouchDB服务器中命名为 my_database .然后,如果您使用URL http://127.0.0.1:5984/_utils/index.html 验证CouchDB中的数据库列表,您将获得以下屏幕截图.
并假设我们选择名为 my_database
现在,使用以下代码获取这些文档的内容.
//Requiring the package var PouchDB = require('PouchDB'); //Creating the database object var db = new PouchDB('http://localhost:5984/my_database'); //Retrieving all the documents in PouchDB db.allDocs({include_docs: true}, function(err, docs) { if (err) { return console.log(err); } else { console.log(docs.rows); } });
将上述代码保存为 remote_bulk_fetch.js .执行时,上述程序为您提供数据库中所有文档的内容,如下所示.
[ { id: '001', key: '001', value: { rev: '3-552920d1ca372986fad7b996ce365f5d' }, doc: { _id: '001', _rev: '3-552920d1ca372986fad7b996ce365f5d', name: 'Raju', age: 23, designation: 'Designer' } }, { id: '002', key: '002', value: { rev: '1-9af15cb11054ebe03a7816bf6c5e4128' }, doc: { _id: '002', _rev: '1-9af15cb11054ebe03a7816bf6c5e4128', name: 'Robert', age: 24, Designation: 'Programmer' } }, { id: '003', key: '003', value: { rev: '1-3033b5a78e915c52fd37325d42eb3935' }, doc: { _id: '003', _rev: '1-3033b5a78e915c52fd37325d42eb3935', name: 'Rahim', age: 25, Designation: 'Programmer' } } ]
以下是更新存储在CouchDB服务器中的名为 my_database 的数据库中存在的所有文档的示例.
//Requiring the package var PouchDB = require('PouchDB'); //Creating the database object var db = new PouchDB('http://localhost:5984/my_database'); //Preparing the document docs = [{_id : '001', _rev: '3-552920d1ca372986fad7b996ce365f5d', age : 24, }, {_id : '002', _rev: '1-9af15cb11054ebe03a7816bf6c5e4128', age : 26, }, {_id : '003', _rev: '1-3033b5a78e915c52fd37325d42eb3935', age : 27}] //Inserting Document db.bulkDocs(docs, function(err, response) { if (err) { return console.log(err); } else { console.log(+"Documents Updated Successfully"); } });
将上述代码保存在名为 Remote_Update_Document.js 的文件中.打开命令提示符并使用node执行JavaScript文件,如下所示.
C:\PouchDB_Examples >node Remote_Update_Document.js
这将更新存储在CouchDB中的名为 my_database 的数据库中存在的所有给定文档的内容,并显示以下消息.
Documents Updated Successfully
现在,如果执行 remote_bulk_fetch.js 程序,您可以看到更新文档的值,如下所示.
[ { id: '001', key: '001', value: { rev: '4-6bc8d9c7a60fed2ed1667ec0740c1f39' }, doc: { _id: '001', _rev: '4-6bc8d9c7a60fed2ed1667ec0740c1f39', age: 25 } }, { id: '002', key: '002', value: { rev: '2-1aa24ce77d96bb9d2a0675cdf1e113e0' }, doc: { _id: '002', _rev: '2-1aa24ce77d96bb9d2a0675cdf1e113e0', age: 26 } }, { id: '003', key: '003', value: { rev: '2-fa113149ba618eda77f73072974a2bc1' }, doc: { _id: '003', _rev: '2-fa113149ba618eda77f73072974a2bc1', age: 27 } } ]